且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Magento 覆盖控制器

更新时间:2023-02-12 12:21:10

  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.