I want to change the layout using the mvc event. I tried the following:
// $event instanceof \Zend\Mvc\MvcEvent $serviceManager = $event->getApplication()->getServiceManager(); $controllerLoader = $serviceManager->get('ControllerLoader'); $controllerLoader->addInitializer(function ($controller) { $controller->layout('layout/example'); // OR THIS $controller->getEvent()->getViewModel()->setTemplate('layout/example'); });
My approbations do not give any error notifications or anything else. Even if layout/example does not exist. Why can I change the layout from the inside of the controller using $this->layout() , but not from the outside using $controller->layout() ?
I also tried this as follows:
// $event instanceof \Zend\Mvc\MvcEvent $serviceManager = $event->getApplication()->getServiceManager(); $renderingStrategy = $serviceManager->get('DefaultRenderingStrategy'); $renderingStrategy->setLayoutTemplate('layout/example');
It also does not produce any errors, but does not change anything.
How to switch layout from outside the controller at runtime?
source share