Magento 覆盖控制器 [英] Magento override controller

查看:19
本文介绍了Magento 覆盖控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做上面的事情.过去我已经覆盖了许多文件......块,模型,助手......但这个我躲过了.

I would like to do the above. Ive overridden many files in the past...block, model, helper....but this one eludes me.

谁能看到我在这里做错了什么:(我编辑了这段代码……现在包括一些建议……)

Can anyone see what im doing wrong here: (ive edited this code...to include some of the recomendations now...)

这是我的文件夹结构(2 个控制器位置作为测试):

Heres my folder structure (2 controller locations as a test):

/Idigital/Idgeneral/etc/config.xml
/Idigital/Idgeneral/controllers/Checkout/CartController.php
/Idigital/Idgeneral/controllers/CartController.php

这是我的 config.xml:

Heres my config.xml:

<?xml version="1.0"?>
<config>
<modules>
    <idigital_idgeneral>
    <version>0.1.0</version>
    </idigital_idgeneral>
</modules>
<global>
<blocks>
        <idgeneral><class>Idigital_Idgeneral_Block</class></idgeneral>
    </blocks>
</global>   

<frontend>
    <routers>
                <checkout>
                    <use>standard</use>
                    <args>
                        <modules>
                            <Idigital_Idgeneral before="Mage_Checkout">Idigital_Idgeneral_Checkout</Idigital_Idgeneral>
                        </modules>
                    </args>
                </checkout>
           </routers>
       <layout>   
        <updates>   
            <idgeneral>   
                <file>idigital.xml</file>   
            </idgeneral>   
        </updates>   
    </layout>
</frontend>
</config>

我已将控制器文件放在 2 个位置进行测试.这是我的第一个控制器文件的顶部:

Ihave placed my controller file in 2 locations to test. And heres the top of my FIRST controller file:

require_once 'Mage/Checkout/controllers/CartController.php';
class Idigital_Idgeneral_Checkout_CartController extends Mage_Checkout_CartController
{


public function testAction()
{  
    var_dump('inside checkout/cart/test');exit; 
}

/**
 * Add product to shopping cart action
 */
public function addAction()
{
    blah...
}

回答我的第二个控制器:

Ans my second controller:

require_once 'Mage/Checkout/controllers/CartController.php';
class Idigital_Idgeneral_CartController extends Mage_Checkout_CartController
{


public function testAction()
{  
    var_dump('inside cart/test');exit; 
}

/**
 * Add product to shopping cart action
 */
public function addAction()
{
    blah...
}

当我访问:/checkout/cart/add我被定向到法师控制器......不是我的本地人.(我在每个中都有 var_dump stmts..所以我可以看到运行了哪个).

When i visit: /checkout/cart/add Im directed to the mage controller...not my local. (i have var_dump stmts in each..so i can see which is ran).

当我访问/checkout/cart/test 时 - 我收到 404当我访问/cart/add 或 cart/test - 我得到一个 404当我访问 idgeneral/cart/test 或 idgeneral/cart/add 时 - 我得到 404

When i visit /checkout/cart/test - i get a 404 When i visit /cart/add or cart/test - i get a 404 when i visit idgeneral/cart/test or idgeneral/cart/add - i get a 404

推荐答案

  1. 创建您的模块文件夹和文件

  1. Create your module folders and files

app/code/local/MyNameSpace/MyModule/etc/config.xml
app/code/local/MyNameSpace/MyModule/controllers/Checkout/CartController.php
app/etc/modules/MyNameSpace_All.xml

  • 编辑/etc/config.xml 并使用以下内容创建 app/code/local/MyNameSpace/MyModule/etc/config.xml:

  • Edit /etc/config.xml and Create app/code/local/MyNameSpace/MyModule/etc/config.xml with the following content:

    <?xml version="1.0"?>
     <config>
    <modules>
        <MyNameSpace_MyModule>
            <version>0.1.0</version>
        </MyNameSpace_MyModule>
    </modules>
    <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_checkout_cart>
                <from><![CDATA[#^/checkout/cart/#]]></from>
                <!--
                    - mymodule matches the router frontname below
                    - checkout_cart matches the path to your controller
    
                    Considering the router below, "/mymodule/checkout_cart/" will be
                    "translated" to "/MyNameSpace/MyModule/controllers/Checkout/CartController.php" (?)
                -->
                <to>/mymodule/checkout_cart/</to>
            </mynamespace_mymodule_checkout_cart>
        </rewrite>
    </global>
    <!--
    If you want to overload an admin controller this tag should be <admin> instead,
    or <adminhtml> if youre overloading such stuff (?)
    -->
    <frontend>
        <routers>
            <mynamespace_mymodule>
                <!-- should be set to "admin" when overloading admin stuff (?) -->
                <use>standard</use>
                <args>
                    <module>MyNameSpace_MyModule</module>
                    <!-- This is used when "catching" the rewrite above -->
                    <frontName>mymodule</frontName>
                </args>
            </mynamespace_mymodule>
        </routers>
    </frontend>
    

    注意:当我覆盖目录/产品控制器时,以上对我不起作用.我不得不使用:

    Note: The above didn’t work for me when I override catalog/product controller. I had to use:

                <from><![CDATA[#^catalog/product/#]]></from>
                <to>mymodule/mycontroller</to>
    

    (注意缺少的前导斜线)

    (notice the missing leading slash)

    自 Magento 1.3 起,您只需将模块添加到前端路由器即可.不再需要重写:

    Since Magento 1.3 you can simply add your module to the frontend router. Rewrites are not neccessary any more:

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

    请注意,before="Mage_Checkout" 将首先加载您的控制器(如果可用),如果没有,则回退到 Magento.

    Please note that before="Mage_Checkout" will load your controller first if available and fall back to Magento’s if not.

    如果控制器在另一个文件夹中,则必须修改代码:
    app/code/local/MyNameSpace/MyModule/controllers/Checkout/CartController.php.

    If the controller is in another folder, you’ll have to modify the code:
    app/code/local/MyNameSpace/MyModule/controllers/Checkout/CartController.php.

    替换

    <MyNameSpace_MyModule before="Mage_Checkout">MyNameSpace_MyModule</MyNameSpace_MyModule>

        <MyNameSpace_MyModule before="Mage_Checkout">MyNameSpace_MyModule_Checkout</MyNameSpace_MyModule>
    

    1. 编辑'controllers/Checkout/CartController.php'

    使用以下内容创建 app/code/local/MyNameSpace/MyModule/controllers/Checkout/CartController.php:(对 indexAction() 的唯一更改是添加一个 error_log() 调用):

    Create app/code/local/MyNameSpace/MyModule/controllers/Checkout/CartController.php with the following content: (the only change to indexAction() is adding an error_log() call):

        <?php
           # Controllers are not autoloaded so we will have to do it manually:
           require_once 'Mage/Checkout/controllers/CartController.php';
           class MyNameSpace_MyModule_Checkout_CartController extends
                           Mage_Checkout_CartController
        {
        # Overloaded indexAction
         public function indexAction() {
        # Just to make sure
        error_log('Yes, I did it!');
        parent::indexAction();
        }
         }
    

    1. 编辑'app/etc/modules/MyNameSpace_All.xml'(这是为了激活你的模块)

    1. Edit 'app/etc/modules/MyNameSpace_All.xml' (This is to activate your module)

    真的当地的

  • 编辑 'app/design/frontend/[myinterface]/[mytheme]/layout/checkout.xml' 并添加以下内容以使用与之前相同的更新句柄:>

  • Edit 'app/design/frontend/[myinterface]/[mytheme]/layout/checkout.xml' and add the following to use the same update handle as before:

     <mynamespace_mymodule_checkout_cart_index>
        <update handle="checkout_cart_index"/>
      </mynamespace_mymodule_checkout_cart_index>
    

    (请注意,这些标签似乎区分大小写.如果这不适合您,请尝试全部使用小写)

    (Note that these tags seem to be case sensitive. Try using all lowercase if this isn’t working for you)

    [by Hendy:当我使用本 Wiki 或此处描述的方法覆盖目录/产品/视图时,我不必执行上述操作.然而,当使用'cms方式'时,我不得不手动更新句柄.]

    [by Hendy: When I override catalog/product/view using the method described in this Wiki or here, I didn’t have to do the above. However, when using the 'cms way', I had to update the handle manually.]

    以上项目对我不起作用(2009-02-19 by Jonathan M Carvalho)

    The above item did not work for me (2009-02-19 by Jonathan M Carvalho)

    我发现要更改的文件是 app/design/frontend/[myinterface]/[mytheme]/layout/mymodule.xml

    I discovered that the file to change is app/design/frontend/[myinterface]/[mytheme]/layout/mymodule.xml

    添加以下几行:


    1. 将浏览器指向/checkout/cart/在您的 PHP 错误日志中,您应该看到是的,我做到了!".

    您需要特别精确地重写正则表达式,因为错误很难发现.

    You need to be extra precise with the rewrite regular expression because errors are very hard to find.

    这篇关于Magento 覆盖控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆