ZendFramework 3でACLを実装しようとしましたが、成功しませんでした。私はこの質問の最初の答え、次の Zf3 Acl:工場登録エラー
:Fatal error: Uncaught exception 'Zend\ServiceManager\Exception\ServiceNotFoundException' with message 'Unable to resolve service "Application\Middleware\AuthorizationMiddleware" to a factory; are you certain you provided it during configuration?'
Module.php
class Module {
const VERSION = '3.0.1';
public function onBootstrap($e) {
$app = $e->getApplication();
$eventManager = $app->getEventManager();
$serviceManager = $app->getServiceManager();
// Register closure on event DISPATCH, call your checkProtectedRoutes() method
$eventManager->attach($e::EVENT_DISPATCH, function ($e) use ($serviceManager) {
$match = $e->getRouteMatch();
$auth = $serviceManager->get(AuthorizationMiddleware::class);
$res = $auth->checkProtectedRoutes($match);
if ($res instanceof Response) {
return $res;
}
}, 1);
}
...
:私はサービスを取得しようとすると、How to implement acl and authorization in Module.php in zf3
は、私は例外を受け取ります}
module.config.php
return[
...
'service_manager' => [
'factories' => [
Middleware\AuthorizationMiddleware::class => Middleware\AuthorizationMiddleware::class,
],
],
...
]
AuthorizationMiddleware
namespace Application\Factory;
use Zend\Authentication\AuthenticationService;
use Zend\Http\PhpEnvironment\Response;
use Zend\Permissions\Acl\Acl;
use Zend\Router\RouteMatch;
class AuthorizationMiddleware {
private $authService;
private $acl;
private $response;
private $baseUrl;
public function __invoke($request, $response) {
$response->getBody()->write('Hello World!');
return $response;
}
/**
* AuthorizationMiddleware constructor.
* @param AuthenticationService $authService
* @param Acl $acl
* @param Response $response
* @param $baseUrl
*/
public function __construct(AuthenticationService $authService, Acl $acl, Response $response, $baseUrl) {
$this->authService = $authService;
$this->acl = $acl;
$this->response = $response;
$this->baseUrl = $baseUrl;
}
public function checkProtectedRoutes(RouteMatch $match) {
if (!$match) {
// Nothing without a route
return null;
}
// Do your checks...
}
}
AuthorizationMiddlewareFactory.php
namespace Application\Factory;
/**
* Description of AuthMiddlewareFactory
*
* @author orion
*/
use Interop\Container\ContainerInterface;
use MyModule\Middleware\AuthorizationMiddleware;
use Zend\Authentication\AuthenticationService;
use Zend\ServiceManager\Factory\FactoryInterface;
class AuthorizationMiddlewareFactory implements FactoryInterface {
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) {
$authService = $container->get(AuthenticationService::class);
$acl = $container->get('Acl'); // II init it in bootstrap(), could be improved
$response = $container->get('Response');
$baseUrl = $container->get('Request')->getBaseUrl();
$authorization = new AuthorizationMiddleware($authService, $acl, $response, $baseUrl);
return $authorization;
return null;
}
}
は間違っては何ですか?
おかげmodule.config.php' you should reference the class
AuthorizationMiddlewareFactoryで
[MCVE]を試して、期待される出力の例を挙げてください。 – Cullub
@clubの回答が更新されました – Orion