在Zend Framework 2中通过URL授予ACL权限

时间:2014-06-27 15:27:31

标签: php permissions zend-framework2 acl

我遵循本教程:http://ivangospodinow.com/zend-framework-2-acl-setup-in-5-minutes-tutorial/ 但是,我想通过自定义网址授予权限,因此我的代码中有一些更改。

module.acl.roles.php

return array(
    'guest'=> array(
        '/home.html',
        '/login.html',
        '/register.html'
    ),
    'admin'=> array(
        '/user/add.html',
        '/user/edit.html',
        '/user/list.html',
    ),
);

module.config.php

return array(
    'router' => array(
        'routes' => array(
            '/home.html' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route' => '/',
                    'defaults' => array(
                        'controller' => 'Application\Controller\Index',
                        'action' => 'index',
                    ),
                ),
            ),
            '/user/add.html' => array(
                'type' => 'Zend\Mvc\Router\Http\Regex',
                'options' => array(
                    'regex' => '/user/add.html',
                    'defaults' => array(
                        'controller' => 'Application\Controller\User',
                        'action' => 'add',
                        'format' => 'html',
                    ),
                    'spec' => '/user/add.%format%',
                ),
            ),
            ...
        ),
    ),
);

但是我收到了这个错误:Route with name "" not found。请给我一些通过网址授予权限的建议和解决方案

谢谢!

1 个答案:

答案 0 :(得分:0)

我真的推荐BjyAuthorize模块(https://packagist.org/packages/bjyoungblood/bjy-authorize)。

但如果您真的想通过yourselft执行此操作,则需要向\Zend\Mvc\MvcEvent::EVENT_ROUTE添加一个监听器。

您可以使用

附加您的听众
$events->attach(MvcEvent::EVENT_ROUTE, array($this, 'myOnRoute'), -1000);

并且在myOnRoute方法中,您可以处理路径

public function myOnRoute(MvcEvent $event) {
  $match      = $event->getRouteMatch();
  $routeName  = $match->getMatchedRouteName();
  // do stuff here (compare to config or whatever)
}