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
If you want to pass values to your layout globally then you can refer this : https://stackoverflow.com/a/21455737/2190889
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',
));
}
Rob Allen has posted a great article about how to access view variables in another view model (e.g.: layout)
Basically the following code, placed inside your layout.phtml, will match your needs:
<?php
$children = $this->viewModel()->getCurrent()->getChildren();
$child = $children[0];
?>
<!-- some HTML -->
<?php echo $this->escape($child->myvar);?>
There are three ways to achieve this in ZF2 (in your controller):
First:
$this->layout()->someVariableName = 'Some value for the variable';
Second:
$this->layout()->setVariable('someVariableName', 'Some value for the variable');
Third:
$this->layout()->setVariables(array(
'someVariableName' => 'Some value for the variable',
'anotherVariable' => 'Some value for another variable',
);
See the add View Variable section below
add it in your module.php file.
You can also do this using view helper.
/**
* Remember to keep the init() method as lightweight as possible
*
* @param \Zend\ModuleManager\ModuleManager $moduleManager
*/
public function init(ModuleManager $moduleManager)
{
$events = $moduleManager->getEventManager();
$events->attach('loadModules.post', array($this, 'modulesLoaded'));
$events->attach('onBootstrap', array($this, 'bootstrap'));
$sharedEvents = $events->getSharedManager();
$sharedEvents->attach(__NAMESPACE__, 'bootstrap', array($this, 'bootstrap'), 100);
$sharedEvents->attach(__NAMESPACE__, 'bootstrap', array($this, 'initializeView'), 100);
$sharedEvents->attach(__NAMESPACE__, 'dispatch', array($this, 'addViewVariables'), 201);
}
/**
*
* @param \Zend\Mvc\MvcEvent $e
*/
public function loadConfiguration(MvcEvent $e)
{
$e->getApplication()->getServiceManager()
->get('ControllerPluginManager')->get('AclPlugin')
->checkAcl($e); //Auth/src/Auth/Controller/AclPlugin
}
/**
*
* @param \Zend\EventManager\EventInterface $e
*/
public function bootstrap(Event $e) {
$eventManager = $e->getParam('application')->getEventManager();
//$app->getEventManager()->attach('dispatch', array($this, 'checkAcl'), 100);
}
/**
* pass variables to layout
*
* @param \Zend\EventManager\EventInterface $e
*/
public function addViewVariables(Event $e)
{
$route = $e->getRouteMatch();
$viewModel = $e->getViewModel();
$variables = $viewModel->getVariables();
if (false === isset($variables['controller'])) {
$viewModel->setVariable('controller', $route->getParam('controller'));
}
if (false === isset($variables['action'])) {
$viewModel->setVariable('action', $route->getParam('action'));
}
$viewModel->setVariable('module', strtolower(__NAMESPACE__));
}
/**
*
* @param \Zend\Mvc\MvcEvent $e
*/
public function initializeView(Event $e)
{
}
and in your layout you can simply access those variables using their name such as $module, $action, $controller according to above example
Have you tried:
$this->layout()->testvar = 'bla';
Using the layout
controller plugin you can retrieve the ViewModel object that is used in layout.phtml
.