Understand Overriding Magento Controller

Today I’m going to discussing a very cool and best Magento method which is known as overriding. Magento basically base on code pool, where all module/plugin are stored in community and core folder. But when we want to override those modules and all then we are using this awesome process which is known as Magento overriding. Basically, there three types of overriding, First one is class overriding another is a model and the last one is controller overriding and also API(soap type) overriding which is a very new concept which I will share you in my next tut.
So let start with basics of Magento overriding. In Magento overriding means we will create same this type file but not in core or community but we will create this in a folder which is known as the local folder.
Model and class overriding are very simple and easy but in Magento controller overriding is typical because in Magento controller overriding Magento will not allow to its autoload.php to extend the controller class to in Magento to anywhere because those are protected by Magento Varien. So for that, we need to use a different or old method by which we will do this.

Here’s the list of files required for the desired setup:

  • app/etc/modules/My_All.xml: It’s a file used to enable our custom module.
  • app/code/local/My/Catalog/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/My/Catalog/controllers/ProductController.php: It’s a custom controller class file which we’ll use to override
    the core “Product” controller class.

First, we need to create a module enabler file. Create a file “app/etc/modules/My_All.xml” and paste the following contents in that file. We’ve used Envato as our module namespace and Catalog as our module name. It’ll enable our “Catalog” module by default.

<?xml version="1.0"?>
<config>
  <modules>
    <My_All>
     <active>true</active>
     <codePool>local</codePool>
    </My_All>
  </modules>
</config>

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

<?xml version="1.0"?>
<config>
  <modules>
    <My_All>
       <version>1.0</version>
    </My_All>
</modules>
<frontend>
  <routers>
    <catalog>
       <args>  
         <modules>
           <My_All before="Mage_Catalog">My_All</My_All>
         </modules>
       </args>
     </catalog>
   </routers>
</frontend>

First, we’ve defined a module version number using the <version> tag. The<catalog> tag is the front controller router name of the “Catalog” core module. It informs the routing system that we want to override the controllers of the “Catalog” module.

Finally, the <My_All before="Mage_Catalog">My_All</My_All> 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.

Finally, the only remaining thing is to define a controller class My_All_ProductController. Let’s create a controller file “app/code/local/My/All/controllers/ProductController.php” and paste the following contents in that file.

<?php
require_once 'Mage/Catalog/controllers/ProductController.php';
class My_All_ProductController extends Mage_Catalog_ProductController
{  
/**
* Product view action */
  public function viewAction()
  {
    echo "test!";
  }
}

We’ve defined an My_All_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.

In the above example, we’ve overridden the viewAction method of the Mage_Catalog_ProductController class. It means that whenever a product detail page is viewed in the front-­end, it’ll call the viewAction method defined in your custom controller class. So you can modify the code defined in that method as per your requirements.

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts