在module.php

时间:2017-08-08 05:32:30

标签: php zend-framework3

我试图在ZF3中获取匹配的路由名称和控制器 - 操作名称 我想在Module.php中找到它,

正如我所尝试的那样 -

public function onBootstrap(MvcEvent $e)
{
   $app = $e->getApplication();
    $em  = $app->getEventManager()->getSharedManager();
    $sm  = $app->getServiceManager();

    $routeMatch = $sm->get('Application')->getMvcEvent()->getRouteMatch();
   }

但它会返回null

提前致谢

1 个答案:

答案 0 :(得分:1)

试试这个 -

public function onBootstrap(MvcEvent $e)
        {

            $app = $e->getApplication();
            $em  = $app->getEventManager()->getSharedManager();
            $sm  = $app->getServiceManager();
    $app->getEventManager()->attach( MvcEvent::EVENT_DISPATCH, function ($e) use ($sm){
                $routeMatch = $sm->get('Application')->getMvcEvent()->getRouteMatch();
                var_dump($routeMatch->getParams());
                var_dump($routeMatch->getMatchedRouteName());exit;
            }, 200);
    }

在bootstrap(函数onBootstrap)上,路由尚未准备好,因此您需要获取某些事件的路由,

代表:(EVENT_DISPATCH,EVENT_RENDER,EVENT_ROUTE)