Magento 1.7 - 如何扩展核心控制器

时间:2012-09-24 13:21:49

标签: php magento

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

应用程序的/ etc /模块/ Custom_Checkout.xml

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

应用程序/本地/客户/结帐的/ 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>

应用程序/本地/客户/结帐/控制器/ 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

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

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

2 个答案:

答案 0 :(得分: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语句的内部和外部添加var_dump$controllerFileName的一些日志记录或$controllerClassName。这通常足以指出文件路径名或类中的小错误(大小写,缺少字符等)来模糊你的模块。

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

答案 1 :(得分:0)

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

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

应拼写:

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