Access ViewModel variables on dispatch event

你。 提交于 2019-12-23 02:38:09

问题


This is a ZF2 question.

I'm trying to change my template, depending on a variable setted on my controller (since is there that im going to decide which template use). In my module onBooststrap i have:

$this->eventManager->attach('dispatch', function($e)
    {
        if (0 === strpos($e->getRouteMatch()->getParam('controller'), __NAMESPACE__, 0))
        {
            $e->getViewModel()->setTemplate('layout');
        }

    }, -100);

and in my controller:

class IndexController extends AbstractActionController
{
    public function indexAction ()
    {
        $view = new \Zend\View\Model\ViewModel();
        $view->setVariable("layout", "layout");
        return $view;
    }
}

but, how do i get access to that view variable "layout", so i can change it in the dispatch event on setTemplate?


回答1:


Looking at the onDispatch method and the MvcEvent class it seems there is a getResult() method, this may contain the result from the controller action.

Otherwise have you looked at the layout controller plugin? This plugin would allow you to change the template:

$this->layout('new layout');

Of course this would need to be inside the controller and most likely require adding the template into the template map.



来源:https://stackoverflow.com/questions/12588278/access-viewmodel-variables-on-dispatch-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!