Magento: Override Frontend Controller

In Magento Extension Development some time you may need to override core controller functionality.

So, There may be a situation in which you want to override AccountController.php file for Customer section.

So what you need to do? Very simple.

Create your new folder with your company name in local directory.

Suppose company name isĀ  MyCompany. And also create following files/folders under MyCompany Flder.

app/code/local/MyCompany/MyExt/controllers/AccountController.php

app/code/local/MyCompany/MyExt/etc/config.xml

Here MyCompany = Company Name and MyExt = Extension Name.

AccountController.php should contain following code.
{code type=php}

require_once(“Mage/Customer/controllers/AccountController.php”);
class MyCompany_MyExt_AccountController extends Mage_Customer_AccountController
{
// override whatever function you want here…
}
{/code}

And config.xml should contain following code.

{code type=xml}

<?xml version=”1.0″?>
<config>
<modules>
<MyCompany_MyExt>
<version>1.0.1</version>
</MyCompany_MyExt>
</modules>
<frontend>
<routers>
<customer>
<args>
<modules>
<MyCompany_MyExt before=”Mage_Customer”>MyCompany_MyExt</MyCompany_MyExt>
</modules>
</args>
</customer>
</routers>
</frontend>
</config>

{/code}

And finally you want 1 MyCompany_MyExt.xml file to activate current extension. Which should placed under app/etc/modules/.

MyCompany_MyExt.xml file should contain following code
{code type=xml}

<?xml version=”1.0″?>
<config>
<modules>
<MyCompany_MyExt>
<active>true</active>
<codePool>local</codePool>
</MyCompany_MyExt>
</modules>
</config>

{/code}

Thats All.

Thanks for reading. :-)

Override Magento Core Controller, Override Magento Front End Controller, Magento Controller Override

    • keyur
    • July 9th, 2010

    nice post….any idea though how to override init() methods of controllers for e.g. _initProduct() method in class Mage_Catalog_ProductController.

    Thanks
    Keyur

  1. No trackbacks yet.

Comment are closed.