user8369078
user8369078

Reputation:

to determine the controller, action , route name in zend framework3 in module.php

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

Upvotes: 0

Views: 346

Answers (1)

Gautam Rai
Gautam Rai

Reputation: 2505

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)

Upvotes: 1

Related Questions