getStaticHelper(action helper) 获取参数但给出错误
我知道如何将参数传递给在操作本身中调用的正常操作助手。但这一次我在 Bootstrap 中使用 HelperBroker::getStaticHelper
$hooks = Zend_Controller_Action_HelperBroker::getStaticHelper('Test');
Zend_Controller_Action_HelperBroker::addHelper($hooks);
我想传递一个参数,所以我添加了中间行
$hooks = Zend_Controller_Action_HelperBroker::getStaticHelper('Test');
$hooks->preDispatch($input);
Zend_Controller_Action_HelperBroker::addHelper($hooks);
,preDispatch 是这样的
public function preDispatch($input){
var_dump($input);
}
奇怪的是 var_dump 显示了输入,但我也收到此错误
Warning: Missing argument 1 for Test::preDispatch(),
Notice: Undefined variable: input
I know how to pass parameters to a normal action helper that's called in the action itself. But this time I'm doing it in the Bootstrap using HelperBroker::getStaticHelper
$hooks = Zend_Controller_Action_HelperBroker::getStaticHelper('Test');
Zend_Controller_Action_HelperBroker::addHelper($hooks);
I want to pass a parameter so I added the middle line
$hooks = Zend_Controller_Action_HelperBroker::getStaticHelper('Test');
$hooks->preDispatch($input);
Zend_Controller_Action_HelperBroker::addHelper($hooks);
and the preDispatch is this
public function preDispatch($input){
var_dump($input);
}
The strange thing is that var_dump shows me the input, but I also get this error
Warning: Missing argument 1 for Test::preDispatch(),
Notice: Undefined variable: input
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
preDispatch() 是在调度循环中调用的钩子。你不应该这样使用它。
Zend_Controller_Action:
此外,此代码不明确:
操作助手是通过 getStaticHelper() 方法调用在助手代理中注册的
preDispatch() is a hook called in dispatch loop. You shouldn't use it like this.
Zend_Controller_Action:
Also this code is ambiguous:
Action helper was registered within helper broker by getStaticHelper() method call
你应该这样做:
preDispatch 认为它的第一个变量是 request 对象 - 这就是它在 ZF 内部的连接方式。
但现在有了新功能,您可以做到:
您不应该为了自己的恶棍计划而弄乱内部方法(即调用它们)。如果你想在助手中注入一些东西,不要直接传递它。添加成员变量和类似
$helper->setImportantThing($thing);
的内容,将其保存到protected $_thing;
中,然后在方法内echo $这->_thing;
You should make it like this:
The preDispatch thinks it's first variable is request object - it's how it'S wired in the ZF's internals.
But now with the new function you can do:
You shouldn't mess with the internal methods (i.e. call them) for your own vilain plans. If you want to inject something in the helper, don't pass it directly. Add member variable and sth like
$helper->setImportantThing($thing);
which will save it intoprotected $_thing;
and then inside the methodecho $this->_thing;