How to get controller name in layout in zf3

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

You need to inject them as view model variables.

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.

1 Like

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.

2 Likes