Override Controllers in Magento

August 14th, 2012 by Laeeq | 1 comment

magento_ecommerce

While working om Magento project, somtimes we need to edit Magemto’s core module files. But it is not right way to play with Magento’s core module file as it can create issues while version update. Magento provides very easy way to overide core modules functionality. you have learned about Override Magento Admin Controller. Here in this post you will learn how to override Magento controller. But controller overriding require some basic knowledge of Cusotm Magento Module development. So before you start override controller, learn how to create custom Magento module.

Here we will override checkout module controller.

Steps to Override Controller in Magento:

Steps 1:

First we will create config.xml file in our custom module. The file will be located at:

app/code/local/MyNameSpace/MyModule/etc/config.xml

  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. <frontend>
  9. <routers>
  10. <checkout>
  11. <args>
  12. <modules>
  13. <MyNameSpace_MyModule before=“Mage_Checkout”>MyNameSpace_MyModule</MyNameSpace_MyModule>
  14. </modules>
  15. </args>
  16. </checkout>
  17. </routers>
  18. </frontend>
  19. </config>

Steps 2:

Add CartController.php file:

app/code/local/MyNameSpace/MyModule/controllers/Checkout/CartController.php

view plaincopy to clipboardprint?

  1. <?php
  2. require_once ‘Mage/Checkout/controllers/CartController.php’;
  3. class MyNameSpace_MyModule_Checkout_CartController extends Mage_Checkout_CartController
  4. {
  5. # Overloaded indexAction
  6. public function indexAction() {
  7. error_log(‘Yes, WE override controller!’);
  8. parent::indexAction();
  9. }
  10. }

Steps 3:

Add MyNameSpace_All.xml  file:

The file will be located as app/etc/modules/MyNameSpace_All.xml. This file will 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>

Above are the simple steps to override Magento’s controllers. Hope it will be helpful for you.

 

You can subscribe to PHPZAG.COM posts by Email

 

Related Topics:

 

 

  1. November 8th, 2012 at 11:00 | #1

    My partner and I stumbled over here by a different page and thought I should check things out.

    I like what I see so now i’m following you. Look forward to exploring your web page repeatedly.

  1. No trackbacks yet.