How to override the Magento 1 controller

How to override Magento controller, must be a good practice whenever we want to add/update a new functionality to current Magento controller functions.

Why overriding controllers?
Controller is the entry point for any incoming request in Magento. To handle this Magento declares numerous functions in the Controller file mainly called actions.

Suppose you want to adjust/ add a small functionality to customer login action. So first we would think about adding this to the core files. Don’t ever do that. Instead of that create a Magento module to achieve this.

I assume you know all the basic things to create a Magento module. So we are directly moving to overriding the module.

Files required

1) app/etc/modules/Writeyourcode_Customer.xml
2) app/code/local/Writeyourcode/Customer/etc/config.xml
3) app/code/local/Writeyourcode/Customer/controllers/AccountController.php

First create the module declaration file, Writeyourcode_Customer.xml
in the following directory app/etc/modules/

 

Next, we need to create a module configuration file. Create “app/code/local/Writeyourcode/Customer/etc/config.xml” and paste the following contents in that file

The tag is the front controller router name of the “Customer” core module. It informs the routing system that we want to override the controllers of the “Customer” module. it should be exactly same as the parent module, otherwise, overriding won’t happen. In some third party modules, it will be written as namespace_modulename. I had to override Aheadworks Onestepcheckout extension(One of the best Magento e-commerce extensions), so I could see they have used in this format(namespace_modulename). Because why I am telling this for this simple thing, I spent more than one day of debugging to override something in checkout functions.

Finally Writeyourcode_Customer tag is used to inform magneto routing system that we have to load the custom module before the core one. don’t worry it will load the functions from core module if it isn’t able to find the details in the custom path.

Next is our controller app/code/local/Writeyourcode/Customer/controllers/AccountController.php

We’ve defined an Writeyourcode_Customer_AccountController class which extends the core Mage_Customer_AccountController controller class of the Customer module. The important thing to note here is that we’ve included the core controller file “Mage/Customer/controllers/AccountController.php” before the class definition as Magento won’t include it automatically. That’s it whenever login page called in the frontend it will be calling this page.

Editorial Staff
 

Editorial Staff at tutsplanet is a dedicated team to write various tutorial articles.