How to overload a controller

Last modified by demonkoryu on Tue, January 10, 2012 07:43
Source|Old Revisions  

(Magento 1.3.0: For those having problems overloading controllers and/or users of LKC_ModularRouters by Lee Saferite, see this forum post.)

In this example we’re overloading Mage_Checkout_CartController::indexAction().

1. Create your module folders and files

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

2. Edit /etc/config.xml

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

  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>

Note: 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)

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 fall back to Magento’s if not.

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

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

with

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

3. Edit ''controllers/Checkout/CartController.php''

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):

  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.         # Just to make sure
  9.         error_log('Yes, I did it!');
  10.         parent::indexAction();
  11.     }
  12. }

4. Edit ''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>

5. Edit ''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.]

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

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

Add the following lines:

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

7. Point your browser to /checkout/cart/

In your PHP error log, you should find ‘Yes, I did it!’.

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

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



 

Magento 2 GitHub Repository

Magento Job Board - Some sort of tag line goes here

Latest Posts| View all Jobs