Magento-从预调度观察者转发到不同的控制器动作 [英] Magento - to forward to different controller action from pre dispatch observer

查看:56
本文介绍了Magento-从预调度观察者转发到不同的控制器动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以基于我可以在控制器预调度事件中检查的特定条件转发到不同的动作(在同一控制器内)?

Is it possible to forward to a different action (within same controller) based on a certain condition that i can check for in the controller pre dispatch event?

例如,要执行的操作是说索引

So for example - the action about to be run is say index

如果我为预调度事件创建一个观察者,如果某个条件为真,那么我想运行一个不同的动作:myAction而不是indexAction

If i create an observer for the pre dispatch event, if a certain condition is true, i would like to run a different action: myAction as opposed to indexAction

推荐答案

您可以在预调度挂钩中执行以下方法.

You could do something as the following method within a pre-dispatch hook.

public function yourMethod($observer)
{
    if ($condition) {
        $request = Mage::app()->getRequest();
        $request->initForward()
            ->setControllerName('controllername')
            ->setModuleName('modulename')
            ->setActionName('actionname')
            ->setDispatched(false);

        return false;
    }
}

但是,如果使用的是自定义控制器,为什么不总是将其路由到例如indexAction()?并在该方法中找出想要_forward()到下一个位置的位置,就像Kalpesh在他的回答中已经提到的那样?性能方面没有区别.

Though, if you are working with a custom controller why not always route it to, for example, the indexAction()? And within that method figure out where you want to _forward() to next, like Kalpesh already mentioned in his answer? Performance wise there is no difference.

这篇关于Magento-从预调度观察者转发到不同的控制器动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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