I want to pass a series of variables to my layout.phtml throughout the whole application(globally). And by that I mean I don\'t wanna use
$this->layout()-
You can pass global variable to your layout from Application/Module.php in ZendFramework 2 using ModuleManager as below:
In your Application/Module.php
Step 1:
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Zend\Mvc\MvcEvent;
Step 2:
class Module implements AutoloaderProviderInterface, ConfigProviderInterface
{
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$eventManager->attach('dispatch', array($this, 'loadConfiguration' ));
}
public function loadConfiguration(MvcEvent $e)
{
$controller = $e->getTarget();
$controller->layout()->YOUR_VARIABLE_NAME = $YOUR_VALUE;
}
// Your remaining code here....
}
Step 3:
In your layout.phtml file
use your variable as $this->YOUR_VARIABLE_NAME
I hope this helps.