如何从Zend Framework 2中的视图脚本获取当前模块/控制器/操作? [英] How to get the current module/controller/action from a view script in Zend Framework 2?

查看:256
本文介绍了如何从Zend Framework 2中的视图脚本获取当前模块/控制器/操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从视图脚本获取当前的模块/控制器/操作名称?

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

推荐答案

您可以使用MvcEvent对象在控制器中检索它,然后将其分配给ViewModel您可以在视图中检索它:

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.

如果您想在多个视图中使用它,那么最好创建并注册一个新的 NAMESPACE 您自己的查看助手。

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

这篇关于如何从Zend Framework 2中的视图脚本获取当前模块/控制器/操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆