Zend Framework 2: Layout Installation Using MvcEvent

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?

+4
source share
2 answers

aww, it was that simple: just name it directly in the event.

 // $event instanceof \Zend\Mvc\MvcEvent $event->getViewModel()->setTemplate('layout/example'); 
+7
source
 //$this instanceof Zend\Mvc\Controller\AbstractActionController $this->layout('layout/example'); 
+2
source

All Articles