Zf3 Acl:出厂登记错误

时间:2017-01-25 13:28:04

标签: php zend-framework2 zend-framework3

我试图在ZendFramework 3中实现ACL而没有成功。

我在这个问题上第一个回答:How to implement acl and authorization in Module.php in zf3

但是当我尝试检索服务时,我收到了异常:

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);
}
...

}

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;
    }

}

有什么问题?

由于

1 个答案:

答案 0 :(得分:1)

module.config.php' you should reference the class AuthorizationMiddlewareFactory中,如下所示:

'service_manager' => [
        'factories' => [
            Middleware\AuthorizationMiddleware::class => Middleware\AuthorizationMiddlewareFactory::class,                        
        ],
    ],

同时检查看似不一致的名称空间。