How to pass variables to layout.phtml globally in ZF2?

前端 未结 6 1927
抹茶落季
抹茶落季 2020-12-05 07:21

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()-         


        
6条回答
  •  我在风中等你
    2020-12-05 07:43

    In your Application/Module.php 1) Create an Dispatch event for zend framework like this:

    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;
    }
    

    2) Now You can access your defined variable in your layout like this: $this->YOUR_VARIABLE_NAME;

提交回复
热议问题