Overriding Core Controller in magento

In my last blog i have just demonstrated simplest way to override core controller . But the best way of overriding core controller

(like Account controller) is by creating a custom module that will override core controller and then we will route our new module for overriding. For example i am taking an example of overriding account controller of Core magento which resides under
app >> code >> core >> mage >> Customer >> Controllers >> AccountController.php
with
app >> code >> local >> Cutehits >> Customer >> controllers >> AccountController.php

Convention1-> Cutehits : It represent Company Name
Convention2-> Customer : It represent Module Name
Step 1:
Create Cutehits_Customer.xml under app/etc/modules (here You should use Company_ModuleName.xml format) with following data.

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

</Cutehits_Customer>
</modules>
</config>

With this file basically we are telling magento that we are registering a new custom module for magento.

Step 2: Now create “config.xml” file under following folder (You need to create some folders under local directory)
app >> code >> local >> Cutehits >> Customer >> etc >> config.xml
with following data

<?xml version="1.0"?>
<config>
<modules>
<Cutehits_Customer>
<version>0.1.0</version>
</Cutehits_Customer>
</modules>

<frontend>
<routers>
<cutehits_customer>
<use>standard</use>
<args>
<module>Cutehits_Customer</module>
<frontName>customer</frontName>
</args>
</cutehits_customer>
</routers>
</frontend>

<global>
<rewrite>
<cutehits_customer_account>
<from><![CDATA[#^/account/#]]></from>
<to>/customer/account/</to>
</cutehits_customer_account>

</rewrite>
</global>
</config>

With this configuration file frontend routing of accountcontroller with our account controller . Here the actual technique of overriding exists with magento.

Step 3: Now write the following code under app >> code >> local >> Cutehits >> Customer >> controllers >> AccountController.php

<?php
require_once 'Mage/Customer/controllers/AccountController.php';
class Cutehits_Customer_AccountController extends Mage_Customer_AccountController
{
// override existing method
public function createPostAction()
{
die("function calling");
}

}
?>

With this script we are just overriding core accountcontroller.php with our accountcontroller.php.
For example here i have written function createPostAction which gets called at the time of registration. So now after doing above steps when you will do any registration then you will see only this createPostAction is getting called not your core file. So you have achieved your target enjoy…

Related posts:

The following two tabs change content below.

Chandra Shekhar

GCP Architect
Chandra Shekhar Pandey is Google certified Cloud engineer, I am Magento2 Trained developer. Having huge experience in designing cloud solution. I have around 12 years of experience with world enterprise IT companies and fortune 500 clients. During my architecture design I am always caring about high availability, fast performance and resilient system. From the programmer background I have huge experience in LAMP stack as well. Throughout my carrier I have worked on Retail, E-Learning, Video... Read More about Chandra Shekhar

Latest posts by Chandra Shekhar (see all)