Zend Framework 2: Establecer diseño usando MvcEvent

I want to change the layout using an mvc event. I've 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 aproaches don't produce any errors notices or something. Not even if layout/example doesn't exist. Why is it possible to change the layout from inside a controller with $this->layout() but not from outside with $controller->layout()?

También lo probé de esta manera:

// $event instanceof \Zend\Mvc\MvcEvent
$serviceManager = $event->getApplication()->getServiceManager();
$renderingStrategy = $serviceManager->get('DefaultRenderingStrategy');
$renderingStrategy->setLayoutTemplate('layout/example');

This also does not throw out any errors but does not change anything.

How can I switch the layout from outside of the controller during runtime?

preguntado el 28 de agosto de 12 a las 13:08

2 Respuestas

aww, it was so easy: simply call it directly on the event..

// $event instanceof \Zend\Mvc\MvcEvent
$event->getViewModel()->setTemplate('layout/example');

Respondido 28 ago 12, 13:08

//$this instanceof Zend\Mvc\Controller\AbstractActionController
$this->layout('layout/example');

respondido 05 nov., 12:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.