简体   繁体   English

ZF2:获取电流控制器

[英]ZF2: Get current controller

I'd like to get the name of current controller in a view helper. 我想在视图助手中获取当前控制器的名称。 I've tried using $this->getEvent()->getRouteMatch()->getParam('controller', 'index'); 我试过使用$ this-> getEvent()-> getRouteMatch()-> getParam('controller','index'); mentioned in another post but is not working. 在另一篇文章中提到但不起作用。 Is it because i'm using it in the helper? 是因为我在助手中使用它吗? Is there another way to get the current controller name? 还有另一种获取当前控制器名称的方法吗?

You can pass HelperPluginManager to your view helper in constructor and then: $vhiewHelperManger->getServiceLocator()->get('application')->getMvcEvent()->getRouteMatch()->getParam('controller')) 您可以在构造函数中将HelperPluginManager传递给视图助手,然后:$ vhiewHelperManger-> getServiceLocator()-> get('application')-> getMvcEvent()-> getRouteMatch()-> getParam('controller'))

But this work only if you wont use forward plugin in controller. 但这仅在您不会在控制器中使用正向插件时才有效。

Zend_Controller_Front::getInstance()->getRequest()->getControllerName()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM