【问题标题】:Magento 1 - Why do controller action predispatch events not fire if the controller is rewritten?Magento 1 - 如果控制器被重写,为什么控制器动作预调度事件不会触发?
【发布时间】:2011-01-08 18:32:46
【问题描述】:

如果控制器被重写,为什么控制器动作预调度事件不会触发?这是store/app/code/core/Mage/Core/Controller/Varien/Action.php的sn-p:

abstract class Mage_Core_Controller_Varien_Action
{
    // [...]
    public function preDispatch()
    {
        // [...]
        if ($this->_rewrite()) {
            return; // [What is the purpose if this?]
        }
        // [...]

        // [This is where my event needs to be firing, but this code never gets 
        // executed because the controller is rewritten]
        Mage::dispatchEvent(
            'controller_action_predispatch_'.$this->getFullActionName(),
            array('controller_action'=>$this)
        );

    }
    // [...]
}

我不知道从哪里着手解决这个问题。以前有人处理过这个吗?

【问题讨论】:

  • afaik 你甚至不能用 magento 重写抽象模型?只在本地/法师中整体替换它们?

标签: php magento event-handling controller extensibility


【解决方案1】:

没有时间测试您所描述的行为是否准确,但如果是,我想这就是 _rewrite 函数中发生的情况,会在该调用之后复制其他非事件代码的操作,并允许 @987654323 @ 在重写后继续会使“坏事”发生。

换句话说,这是控制器重写实现中的一个被忽略的错误,因为现在处理这个问题的首选方法是the routing level。一般来说,当像这样的系统级错误进入 Magento 时,它往往会留在那里,因为当任何事情发生变化时,车主开始依赖破坏的行为并大声尖叫,即使它是一个错误使固定。

如果您无法按照上面链接中的说明重构您的解决方案,您仍然可以使用老式的面向对象编程在控制器类中自己触发事件。将以下内容添加到您的自定义控制器(您要重写的那个)

protected function _rewrite()
{
    //call the parent rewrite method so every that needs
    //to happen happens
    $original_result = parent::_rewrite();

    //fire the event ourselve, since magento isn't firing it
    Mage::dispatchEvent(
        'controller_action_predispatch_'.$this->getFullActionName(),
        array('controller_action'=>$this)
    );      

    //return the original result in case another method is relying on it
    return $original_result;
}

【讨论】:

    【解决方案2】:

    嘿,我不确定(因为我不是很熟悉 Magento 的内部工作原理),但我突然想到 _rewrite() 检查是否正在重定向对该特定操作的调用(重写为mod_rewrite 种方式)到不同的控制器/动作。从这个角度来看,原始操作的事件不会被触发是有道理的,因为整个请求由不同的操作处理。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-24
      • 1970-01-01
      • 2017-12-26
      • 1970-01-01
      相关资源
      最近更新 更多