Set layout variables for use by the (404) error pages in ZF2

前端 未结 2 1451
面向向阳花
面向向阳花 2021-01-22 15:22

At present I set a couple of variables to be used by the app\'s overall layout.phtml, using the onDispatch method of a BaseController, which all my other controllers extend:

相关标签:
2条回答
  • Change the event you're listening for.

    In this case, I'd move this logic to the application bootstrap event or the application render event (I haven't tested this, but it would probably work fine).

    One example, in your Module.php

    public function onBootstrap($e)
    {
        $config = $e->getApplication()->getServiceManager()->get('config');
        //$e->getViewModel()->setVariable();
    }
    

    Haven't tested that commented out line, but it should get you headed in the right direction.

    EDIT: Found an example of using the render event

    public function onBootstrap($e)
    {
        $event = $e->getApplication()->getEventManager();
    
        $event->attach('render', function($e) {
            $config = $e->getApplication()->getServiceManager()->get('config');
            $e->getViewModel()->setVariable('test', 'test');
        });
    } 
    
    0 讨论(0)
  • 2021-01-22 15:39

    (Necro)

    When using onDispatch in a Controller, remember to return the parent with the event and all:

    public function onDispatch(MvcEvent $e)
    {
        // Your code
        return parent::onDispatch($e);
    }
    

    Otherwise, the logic on your Actions in that Controller will be ignored.

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