How to overload a controller

來自官方的論壇 不錯的文章 在此記錄下來

In this example we’re overloading

Mage_Checkout_CartController::indexAction().

 

1. Create your module folders and files

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

2. Edit /etc/config.xml

Go to Magento/app/code/local/MyNameSpace/MyModule/etc/config.xml and paste the following xml into it (comments I’m not a 100% sure about 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 rule could be added to the database instead -->
  10.         <rewrite>
  11.             <!-- This is an identifier for your rewrite that should be unique -->
  12.             <!-- THIS IS THE CLASSNAME IN YOUR OWN CONTROLLER -->
  13.             <mynamespace_mymodule_checkout_cart>
  14.                 <from> <![CDATA[#^/checkout/cart/#]]> </from>
  15.                 <!--
  16.                     - mymodule matches the router frontname below
  17.                     - checkout_cart matches the path to your controller
  18.                    
  19.                     Considering the router below, "/mymodule/checkout_cart/" will be
  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 overloading such stuff (?)
  29.     -->
  30.     <frontend>
  31.         <routers>
  32.             <mynamespace_mymodule>
  33.                 <!-- should be 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/mycontroller</to>

(notice the missing leading slash)]

by AxelH: Since Magento 1.3 you can simply add your module to the frontend router. Rewrites are not neccessary 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 your controller first if available and fallback to Magento’s if not.

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

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

Has to be:

  1.     <MyNameSpace_MyModule before ="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 an error_log() message):

  1. <?php
  2. # Controllers are not autoloaded so we will have to do it manually:
  3. require_once 'Mage/Checkout/controllers/CartController.php' ;
  4. class MyNameSpace_MyModule_Checkout_CartController extends Mage_Checkout_CartController
  5. {
  6.     # Overloaded indexAction
  7.     public function indexAction( )
  8.     {
  9.         # Just to make 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 sufficient for me to overload methods from CartController. I’d like to see some clarification on 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 the expected 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 all lowercase if this isn’t working for you)

[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.]

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, I did it!’.

8. You need to get extra precise with the rewrite regular expression cause this causes a very hard time. In this part.

 <from><![CDATA[#^/checkout/cart/#]]></from>

Will someone please finish this chapter?

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章