How to get controller name in layout in zf3

zend-mvc
zend-view

#1

Please let me know how to get current controller and action name in zend framework 3 in best way


#2

You need to inject them as view model variables.


#3

To simplify the things I wrote a view helper for that purpose

so you can use it or check the code and extract what you need.


#4

I no longer remember where I stole this, which goes in Module.php:

    public function onBootstrap(\Zend\EventManager\EventInterface $event)
    {
        $eventManager = $event->getApplication()->getEventManager();
        $eventManager->attach(MvcEvent::EVENT_ROUTE, function($event){
            $routeMatch = $event->getRouteMatch();
            if ($routeMatch) {
                $event->getApplication()->getMvcEvent()->getViewModel()
                ->setVariables($routeMatch->getParams());                
            }
        });

and then the FQCN of your controller is available to your view as $this->layout()->controller.