i have a layout used by all my views and i need to assign a variable from a controller to this layout , if i use this method on a controller it doesn\'t work :
p
Because ZF2 ViewModel is tree structure, the layout actually is the root node of ViewModel, the ViewModel in your controller will be add as a child node of layout.
You could access layout ViewModel by access MvcEvent, try this in your controller:
public function indexAction()
{
$events = $this->getServiceLocator()->get('Application')->getEventManager();
$events->attach(MvcEvent::EVENT_RENDER, array($this, 'setVariableToLayout'), 100);
}
public function setVariableToLayout($event)
{
$viewModel = $this->getEvent()->getViewModel();
$viewModel->setVariables(array(
'testvar' => 'bla',
));
}