0

I trying to get the matched route name and controller - action name, in ZF3 I want this in Module.php,

as i have tried-

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

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

but it returns null,

Thanks in advance

1
  • 1
    On bootstrap (function onBootstrap), the route is not yet prepared, so you need to get route on some event, for Ex:(EVENT_DISPATCH, EVENT_RENDER, EVENT_ROUTE) Commented Aug 8, 2017 at 5:35

1 Answer 1

1

try this-

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

On bootstrap (function onBootstrap), the route is not yet prepared, so you need to get route on some events,

for Ex:(EVENT_DISPATCH, EVENT_RENDER, EVENT_ROUTE)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.