How to overload controller in Magento?

1. Create your modulefolders and files

  1. Magento/app/code/local/MyNameSpace/MyModule/etc/config.xml
  2. Magento/app/code/local/MyNameSpace/MyModule/ controllers/Checkout/Cart Controller.php
  3. Magento/app/etc/modules/MyNameSpace_All.xml

2. Edit/etc/config.xml

Go to Magento/app/code/local/MyNameSpace/MyModule/etc/config.xmland paste the following xml into it (comments I’m not a 100% sureabout are ending with “(?)”):

  1. <?xml version="1.0"?>
  2. <config>
  3.     <modules>
  4.         <MyNameSpace_MyModule>
  5.             <version>0.1.0 </version>
  6.         </MyNameSpace_MyModule>
  7.     </modules>
  8.     <global>
  9.         <!-- This rewrite rulecould be added to the database instead-->
  10.         <rewrite>
  11.             <!-- This is an identifier for yourrewrite that should be unique-->
  12.             <!-- THIS IS THE CLASSNAME IN YOUR OWNCONTROLLER-->
  13.             <mynamespace_mymodule_checkout_cart>
  14.                 <from> <![CDATA[#^/checkout/cart/#]]> </from>
  15.                 <!--
  16.                  - mymodulematches the router frontname below
  17.                  -checkout_cart matches the path to your controller
  18.                
  19.                 Considering the router below, "/mymodule/checkout_cart/" willbe
  20.                 "translated" to "/MyNameSpace/MyModule/controllers/Checkout/CartController.php" (?)
  21.              -->
  22.                 <to>/mymodule/checkout_cart/ </to>
  23.             </mynamespace_mymodule_checkout_cart>
  24.         </rewrite>
  25.     </global>
  26.     <!--
  27.    If you want to overload an admin-controller this tag should be<admin> instead,
  28.     or <adminhtml> if youre overloadingsuch stuff (?)
  29.     -->
  30.     <frontend>
  31.         <routers>
  32.             <mynamespace_mymodule>
  33.                 <!-- shouldbe set to "admin" when overloading admin stuff (?)-->
  34.                 <use>standard </use>
  35.                 <args>
  36.                     <module>MyNameSpace_MyModule </module>
  37.                     <!-- This is used when"catching" the rewrite above -->
  38.                     <frontName>mymodule </frontName>
  39.                 </args>
  40.             </mynamespace_mymodule>
  41.         </routers>
  42.     </frontend>
  43. </config>

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

  1.                 <from> <![CDATA[#^catalog/product/#]]> </from>
  2.                 <to>mymodule/my controller </to>

(notice the missing leading slash)]

by AxelH: Since Magento 1.3 youcan simply add your module to the frontend router. Rewrites are notneccessary any more:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <config>
  3.     <modules>
  4.         <MyNameSpace_MyModule>
  5.             <version>0.1.0 </version>
  6.         </MyNameSpace_MyModule>
  7.     </modules>
  8.  
  9.     <frontend>
  10.         <routers>
  11.             <checkout>
  12.                 <args>
  13.                     <modules>
  14.                         <MyNameSpace_MyModule before="Mage_Checkout">MyNameSpace_MyModule </MyNameSpace_MyModule>
  15.                     </modules>
  16.                 </args>
  17.             </checkout>
  18.         </routers>
  19.     </frontend>
  20. </config>

Please note that before=”Mage_Checkout” will load yourcontroller first if available andfallback to Magento’s if not.

[Edit by lichal: For me this code doesn’t work, because thecontroller is in the folder (notethe bold folder): ‘Magento/app/code/local/MyNameSpace/MyModule/controllers/Checkout/CartController.php’.In order to fix it the line 14:

  1.     <MyNameSpace_MyModulebefore="Mage_Checkout">MyNameSpace_MyModule </MyNameSpace_MyModule>

Has to be:

  1.     <MyNameSpace_MyModulebefore="Mage_Checkout">MyNameSpace_MyModule_Checkout </MyNameSpace_MyModule>

end Edit by lichal]

3. Edit /controllers/Checkout/CartController.php

Paste the following php code into Magento/app/code/local/MyNameSpace/MyModule/controllers/Checkout/CartController.php(the only change we’re doing to the indexAction() is adding anerror_log() message):

  1. <?php
  2. # Controllers are not autoloaded so we will haveto do it manually:
  3. require_once 'Mage/Checkout/controllers/CartController.php';
  4. classMyNameSpace_MyModule_Checkout_Cart Controller extendsMage_Checkout_Cart Controller
  5. {
  6.     # Overloaded indexAction
  7.     public functionindexAction ( )
  8.     {
  9.         # Just tomake sure
  10.         error_log ( 'Yes, I did it!' );
  11.         parent:: indexAction ( );
  12.     }
  13. }

4. Edit Magento/app/etc/modules/MyNameSpace_All.xml

(This is to activate your module)

  1. <?xml version="1.0"?>
  2. <config>
  3.     <modules>
  4.         <MyNameSpace_MyModule>
  5.             <active>true </active>
  6.             <codePool>local </codePool>
  7.         </MyNameSpace_MyModule>
  8.     </modules>
  9. </config>

[Edit 2009-07-26 by PhilFreo: Steps 1-4 alone were sufficientfor me to overload methods from CartController. I’d like to see some clarificationon exactly what steps 5+ are doing and when they are needed.]


[Edit 2009-07-29 by hexdoll: used steps 1-4 to override the customer/account controller, the error_log occurs but theexpected interface templates are not shown]

5. Edit Magento/app/design/frontend/[myinterface]/[mytheme]/layout/checkout.xml

Add the following to use the same update handle as before:

  1. <mynamespace_mymodule_checkout_cart_index>
  2.     <update handle="checkout_cart_index"/>
  3. </mynamespace_mymodule_checkout_cart_index>

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

[by Hendy: When I overridecatalog/product/view using the method described in this Wikior here, I didn’t have to do the above. However,when using the 'cms way', I had to update the handlemanually.]

6. The above item do not worked for me (updated: 2009-02-19 by:Jonathan M Carvalho)

After loose so many hours I discovery that the file to change is“Magento/app/design/frontend/[myinterface]/[mytheme]/layout/mymodule.xml”

Update 2009-06-17 by Gabriiiel : added the good syntax(mynamespace_mymodule_checkout_cart_index)

Add the following lines:

  1. <mynamespace_mymodule_checkout_cart_index>
  2.     <update handle="checkout_cart_index"/>           
  3. </mynamespace_mymodule_checkout_cart_index>

Using version 1.2.1

7. Point your browser to/checkout/cart/

Take a look in your php error log and you should find ‘Yes, Idid it!’.

8. You need to get extra precise with the rewrite regularexpression cause this causes a very hard time. In thispart.

 <from><![CDATA[#^/checkout/cart/#]]></from>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值