【问题标题】:OnepageController is not overloading/rewriting with my custom controllerOnepageController 没有用我的自定义控制器重载/重写
【发布时间】:2012-10-23 14:39:02
【问题描述】:

我正在尝试使用本地池中的自定义控制器重载/重写核心 Onepagecontroller,但它不起作用。 我正在使用 Magento 1.5.1

这是我的文件结构和代码:

控制器文件: \app\code\local\Odc\Mycheckout\controllers\OnepageController.php

    require_once 'Mage/Checkout/controllers/OnepageController.php';

    class Odc_Mycheckout_OnepageController extends Mage_Checkout_OnepageController
    {   
        public function indexAction()
        {
            echo "This controller has been overridden.";
        }
    }

config.xml 文件: \app\code\local\Odc\Mycheckout\etc\config.xml

    <?xml version="1.0"?>
    <config>
        <modules>
            <Odc_Mycheckout>
                <version>0.0.1</version>
            </Odc_Mycheckout>
        </modules>
        <global>
            <controllers>
                <Mage_Checkout>
                    <rewrite>
                        <onepage>Odc_Mycheckout_Onepage</onepage>
                    </rewrite>
                </Mage_Checkout>
            </controllers>
        </global>
        <frontend>
            <routers>
                <mycheckout>
                   <args>
                        <modules>
                            <Odc_Mycheckout before="Mage_Checkout">Odc_Mycheckout</Odc_Mycheckout>
                        </modules>
                   </args>
                </mycheckout>
            </routers>
        </frontend>
    </config>

Odc_Mycheckout.xml 文件: \app\etc\module\Odc_Mycheckout.xml

    <?xml version="1.0"?>
    <config>
      <modules>
        <Odc_Mycheckout>
          <active>true</active>
          <codepool>local</codepool>
        </Odc_Mycheckout>
      </modules>
    </config>

【问题讨论】:

    标签: magento magento-1.5


    【解决方案1】:

    CamelCase 又来了。

    Odc_Mycheckout.xml 文件: \app\etc\module\Odc_Mycheckout.xml

        <?xml version="1.0"?>
        <config>
          <modules>
            <Odc_Mycheckout>
              <active>true</active>
              <codePool>local</codePool> <!-- Capital P in pool -->
    

    另外,在你的模块配置文件中:

        <frontend>
            <routers>
                <checkout> <!-- must match the router config you are trying to override -->
                   <args>
                        <modules>
                            <Odc_Mycheckout before="Mage_Checkout">Odc_Mycheckout</Odc_Mycheckout>
                        </modules>
                   </args>
                </checkout>
            </routers>
        </frontend>
    

    编辑:

    要在重写的控制器无法正常工作时进行故障排除,这有助于回归基础。一种方法是使用与Mage_Core_Controller_Varien_Router_Standard 相同的方法。在脚本中 test.php:

    <?php
    
    ini_set('display_errors',1);
    include 'app/Mage.php';
    Mage::setIsDeveloperMode(true);
    Mage::app();
    
    $module     = 'Odc_Mycheckout';
    $controller = 'Onepage';
    
    $router     = new Mage_Core_Controller_Varien_Router_Standard;
    $filename   = $router->getControllerFileName($module,$controller);
    $classname  = $router->getControllerClassName($module,$controller);
    
    include $filename;
    
    $controller = Mage::getControllerInstance(
        $classname,
        Mage::app()->getRequest(),
        Mage::app()->getResponse()
    );
    
    var_dump($controller); //should be a  class
    

    【讨论】:

    • 感谢您的回答,但在给定更改后仍然无法正常工作。另请查看 config.xml 中的 标记。这是正确的方法吗?
    • 不,任何核心 Magento 代码都不会评估 xpath (global/controllers)。尝试向您的自定义控制器类添加另一个操作,并通过 URL 调用 that 操作。另外,请注意声明文件的路径应该是 app/etc/modules/ 而不是 app/etc/module/
    • 是的,声明文件位于 app/etc/modules/。但是通过 URL 我收到了这个 Forbidden 错误:您无权访问此服务器上的 /onedaycheckout/app/code/local/Odc/Mycheckout/Checkout/controllers/sdasd.php。
    • 请求如何解析为 sdasd.php?这对于标准的 Magento 来说是不可能的。
    • 抱歉弄错了。该网址是 /onedaycheckout/app/code/local/Odc/Mycheckout/Checkout/controllers/OnepageController.php 并显示相同的 Forbidden 错误。
    【解决方案2】:

    以下标签不需要。

       <global>
            <controllers>
                <Mage_Checkout>
                    <rewrite>
                        <onepage>Odc_Mycheckout_Onepage</onepage>
                    </rewrite>
                </Mage_Checkout>
            </controllers>
        </global>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-31
      • 1970-01-01
      相关资源
      最近更新 更多