简体   繁体   English

得到当前控制器

[英]get current controller

in a function I want to reach current controller: 在我希望到达当前控制器的函数中:

$front = Zend_Controller_Front::getInstance();

this only gives a handler but not current controller. 这只给出了一个处理程序但不是当前控制器。

I changed the code from function to inside of controller. 我将代码从函数更改为控制器内部。 and asked their origins both the handler I got from getInstance and this 并询问他们的起源是我从getInstance得到的处理程序和这个

var_dump(get_class($front), get_class($this));

I get: 我明白了:

string 'Zend_Controller_Front' (length=21)
string 'IndexController' (length=15)

How can I reach real initiated front controller? 如何才能到达真正启动的前端控制器?

I cant pass as a parameter, because this function is used trillion times. 我不能作为参数传递,因为这个函数使用了数万亿次。

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

Possible with: 可能的:

$front = Zend_Controller_Front::getInstance()
$request = $front->getRequest();
$module = ucfirst($request->getModuleName());
$controller = ucfirst($request->getControllerName());

$instance = new $module . '_' . $controller . 'Controller';

In Action Helper: 在行动助手:

$instance = $this->getActionController();

But , this probably means that's something wrong with your architecture. 但是 ,这可能意味着您的架构出了问题。
You should move the common code you need to action helper, service or model. 您应该将所需的公共代码移动到操作助手,服务或模型。

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

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