+91 796-525-5075 ( 11AM To 7PM-IST )

Overriding Controller In Magento

Magento controller is a backbone of the Magento routing system which has responsible for handling incoming requests. The controller handles every request by different actions in the form of controller class methods. Action method defines related application logic which interacts with views and models to produce the output of that particular page. Sometime we need to change the flow of controller execution to inject in our custom code or change the core code.

Suppose we have a module called Lr_Custom present at local directory app/code/local/Lr/Custom and we want rewrite below model classes

Overriding Controller

There are two process available:

  1. Basic Old process.
  2. Upgrade Process from Magento CE 1.5.0.

First I will describe second process. we’ll see how to override a “Product” controller class of the core “Catalog” module. Overriding works by setting up certain XML tags as per the conventions of Magento.So first of all let check which files we need to add in our module.

Suppose we want to rewrite the method galleryAction of class Mage_Catalog_ProductController,then we need these below files

  • app/etc/modules/Lr_Custom.xml: It’s a file used to enable our custom module.
  • app/code/local/Lr/Custom/etc/config.xml: It’s a module configuration file in which we’ll set up controller class overriding using certain tags as per the Magento conventions.
  • app/code/local/Lr/Custom/Catalog/controllers/ProductController.php: It’s a custom controller class file which we’ll use to override the core “Product” controller class.

and we can rewrite by below code in confi.xml file.

In the config node its child has to define whether we are changing the frontend or admin file and we define router node that will
override core catalog module with its arguments. The <catalog> tag is the front controller router name of the “Catalog” core module. It
notify to the routing system that we want to override the controllers of the “Catalog” module.

In the bottom node goes the current module frontend name (Lr_Custom in my example) with the “before” or “after” attribute name with the
value of which module is being overridden (Mage_Catalog) and our own module name inside the tags (Lr_Custom).

The <Lr_Custom before=”Mage_Catalog”>Lr_Custom</Lr_Custom> tag is used to inform the Magento routing system that it should load your
custom module’s controller files if available. Of course, it’ll load the core files of the “Catalog” module if it isn’t able to find them
under your module.

class defination would be

We’ve defined an Lr_Custom_ProductController class which extends the core Mage_Catalog_ProductController controller class of the
Catalog module. The important thing to note here is that we’ve included the core controller file
Mage/Catalog/controllers/ProductController.php” before the class definition as Magento won’t include it automatically.

dir: app/etc/modules/Lr_Custom.xml

dir: app/code/local/Lr/Custom/etc/config.xml

Rewrite Class: