覆盖Magento社区模块控制器的问题

时间:2014-04-16 23:48:25

标签: php magento module controller override

首先,这里的答案非常有用:Overriding a Magento Controller in community extention

然而,尽管通过了"测试"在答案中提到,我仍然有一个模块控制器覆盖,只是拒绝工作。这就是我所拥有的:

\应用\代码\本地\公司\ OnepageCheckout \等\ config.xml中

<?xml version="1.0"?>
<config>
    <modules>
        <Company_OnepageCheckout>
            <version>0.0.1</version> 
        </Company_OnepageCheckout>
    </modules>
    <frontend>
        <routers>
            <onepagecheckout>
                <args>
                    <modules>
                        <company_onepagecheckout before="IWD_OnepageCheckout">Company_OnepageCheckout</company_onepagecheckout>
                    </modules>
                </args>
            </onepagecheckout>
        </routers>
    </frontend>
</config>

\应用\代码\本地\公司\ OnepageCheckout \控制器\ IndexController.php

<?php

require_once(Mage::getModuleDir('controllers','IWD_OnepageCheckout').DS.'IndexController.php');

class Company_OnepageCheckout_IndexController extends IWD_OnepageCheckout_IndexController
{
    function indexAction() {
        die('Hello world!');
    }
}

\应用\等\模块\ Company_OnepageCheckout.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Company_OnepageCheckout>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <IWD_OnepageCheckout />
            </depends>
        </Company_OnepageCheckout>
    </modules>
</config>

此模块用于覆盖位于\app\code\community\IWD\OnepageCheckout\[...]

的模块

正如本文顶部所提到的,我已经按照其他问题的建议使用了Reflection类,并且可以在社区模块之前的数组中首先看到我的模块。但是,当我访问mysite.com/onepagecheckout时,我仍然看到社区模块,并且无法使用我的文件。

我的预感是,它可能与我刚刚缺失的模块名称的大小写有关。

2 个答案:

答案 0 :(得分:2)

只需更改

<company_onepagecheckout before="IWD_OnepageCheckout">Company_OnepageCheckout</company_onepagecheckout> 

<Company_OnepageCheckout before="IWD_OnepageCheckout">Company_OnepageCheckout</Company_OnepageCheckout>

如果它不起作用则使用

<global>
        <!-- This rewrite rule could be added to the database instead -->
        <rewrite>
            <!-- This is an identifier for your rewrite that should be unique -->
            <!-- THIS IS THE CLASSNAME IN YOUR OWN CONTROLLER -->
            <mynamespace_mymodule_onepage_iwd>
                <from><![CDATA[#^/onepagecheckout/index/#]]></from>
                <!--
                    - mymodule matches the router frontname below
                    - checkout_cart matches the path to your controller

                    Considering the router below, "/mymodule/checkout_car/" will be
                    "translated" to "/MyNameSpace/MyModule/controllers/Checkout/CartController.php" (?)
                -->
                <to>/comonepagecheckout/index/</to>
            </mynamespace_mymodule_onepage_iwd>
        </rewrite>
    </global>
<frontend>
        <routers>
            <comonepagecheckout>
                <!-- should be set to "admin" when overloading admin stuff (?) -->
                <use>standard</use>
                <args>
                    <module>Company_OnepageCheckout</module>
                    <!-- This is used when "catching" the rewrite above -->
                    <frontName>comonepagecheckout</frontName>
                </args>
            </comonepagecheckout>
        </routers>
    </frontend>

See more

答案 1 :(得分:1)

最好的办法是将一些调试代码放入标准路由器中(当然暂时)。

#File: app/code/core/Mage/Core/Controller/Varien/Router/Standard.php
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;
}

每个返回false都是路由器认为已配置的控制器并根据名称不正确拒绝它,无法找到或包含该文件等的实例。