Call-back icon  Enterprise Sales: +1.310.775.2674 (N. America)   +44 20.3286.4137 (UK)

Magento

eCommerce Software for Online Growth

How to overload a controller

Last modified by Gabriiiel on Tue, June 16, 2009 21:58
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. 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>

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>

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)

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>



 

Magento User Guide

Magento Enterprise Available now

Professional Services from the Magento Team

Magento Job Board - Some sort of tag line goes here

Latest Posts| View all Jobs
© Copyright 2009 Varien. Magento, eCommerce software, is a trademark of Irubin Consulting Inc. DBA Varien
Privacy Policy|Terms of Service
Magento Community Count
108639 users|336 users currently online|199705 forum posts