Overriding controller class in magento

17 Jun
Sometime we have to run in the situation where we need to overwrite the core module. Note if you make changes in the core file then it will trouble you at the time of up-gradation.So I came up with the new blog which gives you the idea about how to override controllers in magento.Now suppose we want to override the checkout/cart controller. In this case, we will have to edit just two files in our custom module.
Suppose our custom module is present in the namespace ‘Donebynone‘ and our module is named as ‘Mukund‘.

First Step:-

Open the app\code\local\Donebynone\Mukund\etc\config.xml and edit it as follows:
The name of the core module to be overridden is written in between the router tags. Here we want to override the checkout module so checkout is to be wrapped in <routers> tags.

After this we will tell Magento to call our custom module before the Mage/Checkout module.

<?xml version=”1.0″ encoding=”UTF-8″?>
<config>
<modules>
<Donebynone_Mukund>
<version>0.0.1</version>
</Donebynone_Mukund>
</modules>
<frontend>
<routers>
<checkout><!– Name of core module to be overridden  –>
<args>
<modules>
<Donebynone_Mukund before=”Mage_Checkout”>Donebynone_Mukund</Donebynone_Mukund>
<!– Above line will tell Magento to call our custom module before the Mage/Checkout module –>
</modules>
</args>
</checkout>
</routers>
</frontend>
</config>

Final Step:-

Now we need to create the controller file to be overridden, CartController.php in our case.
(app\code\local\Donebynone\Mukund\controllers\CartController.php)

<?php

/*
Include the core file to be overridden
*/
require_once(“Mage/Checkout/controllers/CartController.php”);

/*
* Extend the core controller in our custom controller.
*/
class Donebynone_Mukund_CartController extends Mage_Checkout_CartController
{
//Write your fucntion here..

public function indexAction()
{
echo “This controller has been overridden.”;
}
}

?>

Now its your turn to do the job…go ahead with confidence.

Need any help on this….write your comments…

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s