How to get the current module/controller/action from a view script in Zend Framework 2?

淺唱寂寞╮ 提交于 2019-12-25 07:51:35

问题


Is it possible to get the current module/controller/action name from a view script? How can I do it?


回答1:


You could retrieve it in your controller using the MvcEvent object and then assign it to the ViewModel so you can retrieve it in the view:

public function indexAction()
{
   return new ViewModel(
            array(
                'controller' => $this->getEvent()->getRouteMatch()->getParam('controller'),
                'action' => $this->getEvent()->getRouteMatch()->getParam('action')
            )
        );
}

The module name is a bit trickier, but you could for instance get it using the NAMESPACE or just parse it from the controller name.

If you want to use this in multiple views then it's probably better to create and register your own view-helper.



来源:https://stackoverflow.com/questions/15960987/how-to-get-the-current-module-controller-action-from-a-view-script-in-zend-frame

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