getStaticHelper(action helper) 获取参数但给出错误

发布于 2024-10-21 13:35:19 字数 727 浏览 1 评论 0原文

我知道如何将参数传递给在操作本身中调用的正常操作助手。但这一次我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

朕就是辣么酷 2024-10-28 13:35:19

preDispatch() 是在调度循环中调用的钩子。你不应该这样使用它。

Zend_Controller_Action:

/**
 * Dispatch the requested action
 *
 * @param string $action Method name of action
 * @return void
 */
public function dispatch($action)
{
    // Notify helpers of action preDispatch state
    $this->_helper->notifyPreDispatch();

    ...
    $this->_helper->notifyPostDispatch();
}

此外,此代码不明确:

Zend_Controller_Action_HelperBroker::addHelper($hooks);

操作助手是通过 getStaticHelper() 方法调用在助手代理中注册的

preDispatch() is a hook called in dispatch loop. You shouldn't use it like this.

Zend_Controller_Action:

/**
 * Dispatch the requested action
 *
 * @param string $action Method name of action
 * @return void
 */
public function dispatch($action)
{
    // Notify helpers of action preDispatch state
    $this->_helper->notifyPreDispatch();

    ...
    $this->_helper->notifyPostDispatch();
}

Also this code is ambiguous:

Zend_Controller_Action_HelperBroker::addHelper($hooks);

Action helper was registered within helper broker by getStaticHelper() method call

走过海棠暮 2024-10-28 13:35:19

你应该这样做:

class MyHelper extends Zend_Controller_Action_Helper
{
    const BAR = false;

    public function preDispatch($request)
    {
        $this->ifBarExit(self::BAR);
    }

    public function ifBarExit($barValue)
    {
        if ($barValue) {
            exit('Bar was true!');
        }
    }
}

preDispatch 认为它的第一个变量是 request 对象 - 这就是它在 ZF 内部的连接方式。

但现在有了新功能,您可以做到:

 $helper = Zend_Controller_Action_HelperBroker::getStaticHelper('MyHelper');
 $variable = true;
 $helper->ifBarExit($variable); //won't exit

您不应该为了自己的恶棍计划而弄乱内部方法(即调用它们)。如果你想在助手中注入一些东西,不要直接传递它。添加成员变量和类似 $helper->setImportantThing($thing); 的内容,将其保存到 protected $_thing; 中,然后在方法内 echo $这->_thing;

You should make it like this:

class MyHelper extends Zend_Controller_Action_Helper
{
    const BAR = false;

    public function preDispatch($request)
    {
        $this->ifBarExit(self::BAR);
    }

    public function ifBarExit($barValue)
    {
        if ($barValue) {
            exit('Bar was true!');
        }
    }
}

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:

 $helper = Zend_Controller_Action_HelperBroker::getStaticHelper('MyHelper');
 $variable = true;
 $helper->ifBarExit($variable); //won't exit

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 into protected $_thing; and then inside the method echo $this->_thing;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文