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

前端 未结 6 1914
抹茶落季
抹茶落季 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:29

    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.

    0 讨论(0)
  • 2020-12-05 07:33

    Why not use inheritance to abstract repeatable configuration via a base controller that sets the layout variables. Since reuse is the main concern here, basic OOP should suffice. Zend doesn't have to do everything :).

    0 讨论(0)
  • The best and cleanest way is using a ViewHelper, as Orochi suggests. Here some instructions to create your own ViewHelper: http://framework.zend.com/manual/2.2/en/modules/zend.view.helpers.advanced-usage.html. It's not too complex ;)

    However your problem could be resolved also in another way. Supposing the variables you need contain values provided by services already present in your application (and then exposed by ZF2 ServiceManager), you could add some lines on the "onBoostrap" function inside your Module.php (e.g. on Application module).

    Here an example:

    public function onBootstrap($e) {
    
        $serviceManager = $e->getApplication()->getServiceManager();
        $viewModel = $e->getApplication()->getMvcEvent()->getViewModel();
    
        $myService = $serviceManager->get('MyModule\Service\MyService');
    
        $viewModel->someVar = $myService->getSomeValue();
    
    }
    

    In this way you write the assignment in only one place. The variable is then accessible as usual:

    $this->layout()->someVar;
    
    0 讨论(0)
  • 2020-12-05 07:43

    This is exactly what you want...

    // Do this inside your Controller before you return your ViewModel
    $this->layout()->setVariable('stack', 'overflow');
    
    // Then inside your layout.phtml
    echo $this->stack;
    

    No view helpers, no event manager, no service manager or other class method juggling. Why do people on Stack make things so darn complicated sometimes?

    0 讨论(0)
  • 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;

    0 讨论(0)
  • 2020-12-05 07:53

    Create your own ViewHelper for that.

    EDIT:

    That's how I do it: Inside your Module's src folder, I create new View folder then in this View folder, I create another folder called Helper. In this folder i create my helper classes. So, for example, I create Navigation.php file. In this file I create a class called Navigation, which extends from AbstractHelper (use Zend\View\Helper\AbstractHelper). Then I write code inside public function __invoke(), which also returns some result. You also have to add your view helper class to 'view_helpers' inside module's config/module.config.php, something like that:

    'view_helpers' => array(
        'invokables' => array(
            'nav' => 'Application\View\Helper\Navigation',
        ),
    ),
    

    Then you can call your 'nav' as a method anywhere inside your view's or layout, for example nav();?>

    0 讨论(0)
提交回复
热议问题