简体   繁体   English

在ZF2路由配置中传递自定义变量?

[英]Pass custom variables in ZF2 routing config?

I'm thinking about the best way of implementing ACL. 我正在考虑实现ACL的最佳方法。 So - I need to protect certain routes. 所以-我需要保护某些路线。 Some routes would be accessible only to users, some to guests and some to admins. 有些路线仅对用户开放,有些对访客,对管理员开放。

It seems like the best way of doing it would be by adding a $role variable in the routing config. 似乎最好的方法是在路由配置中添加$ role变量。 So then I'd attach to post-route event, fetch the routeMatch and I would see if this user can enter this route. 因此,我将附加到路由后事件,获取routeMatch,然后查看该用户是否可以输入此路由。

How can I do that? 我怎样才能做到这一点? Can I simply inject extra varibles like this: 我可以像这样简单地注入额外的变量:

'router' => array(
    'routes' => array(
        'route1' => array(
            'type' => 'Zend\Mvc\Router\Http\Regex',
            'options' => array(
                'regex' => '/some/route/1',
                'defaults' => array(
                    'controller' => 'Subscriber\Controller\View',
                    'action'     => 'route1',
                    'role'       => 'user', //extra
                ),
                'spec' => '/some/route/1',
            ),
        ),
        'route2' => array(
            'type' => 'Zend\Mvc\Router\Http\Regex',
            'options' => array(
                'regex' => '/some/route/2',
                'defaults' => array(
                    'controller' => 'Subscriber\Controller\View',
                    'action'     => 'route2',
                    'role'       => 'guest', //extra
                ),
                'spec' => '/some/route/2',
            ),
        ),
        //other routes....
    ),
),

Yes you can just add the router key like you have 是的,您只需添加路由器密钥即可

'defaults' => array(
    'controller' => 'Subscriber\Controller\View',
    'action'     => 'route1',
    'role'       => 'user', //extra
),

And then you can checkit like this 然后你可以像这样检查

public function onBootstrap(MvcEvent $e) {
    $application            = $e->getApplication();
    $eventManager = $application->getEventManager();
    $eventManager->attach(MvcEvent::EVENT_ROUTE, function(MvcEvent $e) {
        $e->getRouteMatch()->getParam('role');
    });
}

There are however modules made for this For example bjyoungblood/BjyAuthorize which works with ZfcUser 但是,为此有一些模块,例如与ZfcUser一起使用的bjyoungblood / BjyAuthorize

You should take a look at : ZfcRbac . 您应该看看: ZfcRbac It's well documented. 有据可查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM