gpt4 book ai didi

php - Magento 1 - 如果 Controller 被重写,为什么 Controller Action 预发送事件不会触发?

转载 作者:行者123 更新时间:2023-12-04 20:35:02 25 4
gpt4 key购买 nike

如果 Controller 被重写,为什么 Controller Action 预分配事件不会触发?这是 store/app/code/core/Mage/Core/Controller/Varien/Action.php 的片段:

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)
);

}
// [...]
}

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

最佳答案

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

换句话说,它是被忽略的 Controller 重写实现中的一个错误,因为处理这个问题的首选方法现在位于 the routing level。 .一般来说,当像这样的系统级错误进入 Magento 时,它往往会留在那里,因为购物车所有者开始依赖损坏的行为并在 任何 更改时大声尖叫,即使它是一个错误使固定。

如果您不能按照上面链接中的描述重构您的解决方案,您仍然可以使用老式的面向对象编程在 Controller 类中自己触发事件。将以下内容添加到您的自定义 Controller (您要重写的 Controller )

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;
}

关于php - Magento 1 - 如果 Controller 被重写,为什么 Controller Action 预发送事件不会触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4635471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com