【问题标题】:Magento 1.7 - How to extend core controllerMagento 1.7 - 如何扩展核心控制器
【发布时间】:2012-09-24 13:21:49
【问题描述】:

我正在尝试使用此设置扩展 OnepageController:

app/etc/modules/Custom_Checkout.xml

<config>
    <modules>
        <Custom_Checkout>
            <active>true</active>
            <codePool>local</codePool>
        </Custom_Checkout>
    </modules>
</config>

app/local/Custom/Checkout/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Custom_Checkout>
            <version>0.0.1</version>
        </Custom_Checkout>
    </modules>
    <frontend>
        <routers>
            <checkout>
                <args>
                    <modules>
                        <custom_checkout before="Mage_Checkout">Custom_Checkout</custom_checkout>
                    </modules>
                </args>
            </checkout>
        </routers>
    </frontend>
</config>

app/local/Custom/Checkout/controllers/OnepageController.php

require_once("Mage/Checkout/controllers/OnepageController.php");

class Custom_Checkout_OnepageController extends Mage_Checkout_OnepageController
{

    public function indexAction()
    {
    echo "Index overidden";
    }

}

我见过这些: Extend magento core controller (Checkout/OnepageController)

http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/how_to_overload_a_controller

还有一些我无法发布的内容,但上述方法似乎都不起作用。它只是不会覆盖控制器。

关于为什么这不是覆盖的任何想法?

【问题讨论】:

  • 你的app/local/ 路径应该是app/code/local
  • 我遇到了同样的问题,调试后我发现有另一个安装的模块已经重载了 OnepageCntroller.php,这就是没有加载正确代码的原因。最好同时检查其他已安装的模块。

标签: php magento


【解决方案1】:

不幸的是,这里有无数可能出错的地方,而且您的帖子中没有足够的信息来追踪它们。这是一个调试技巧,而不是答案。看看_validateControllerClassName 函数。

protected function _validateControllerClassName($realModule, $controller)
{
    $controllerFileName = $this->getControllerFileName($realModule, $controller);
    if (!$this->validateControllerFileName($controllerFileName)) {
        return false;
    }

    $controllerClassName = $this->getControllerClassName($realModule, $controller);
    if (!$controllerClassName) {
        return false;
    }

    // include controller file if needed
    if (!$this->_includeControllerClass($controllerFileName, $controllerClassName)) {
        return false;
    }

    return $controllerClassName;
}

每个return false 是Magento 可能决定不使用您的控制器类进行请求的状态。尝试在 if 语句的内部和外部添加一些日志记录或 $controllerFileName$controllerClassNamevar_dumping。这通常足以指出文件路径名或类中的小错误(大小写、缺少字符等)来确定您的模块。

如果你没有看到任何与Custom_Checkout 相关的信息,这意味着 Magento 看不到你的模块,你应该开始调试它。

【讨论】:

    【解决方案2】:

    我认为解决方案是有正确的案例。而不是:

    <custom_checkout before="Mage_Checkout">Custom_Checkout</custom_checkout>
    

    应该拼写:

    <Custom_Checkout before="Mage_Checkout">Custom_Checkout</Custom_Checkout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-09
      • 2018-11-21
      • 2015-01-19
      • 1970-01-01
      • 2012-08-10
      相关资源
      最近更新 更多