Override magento controller

Modifying magento core file is not at all good idea. All core files that we have modified will be smashed while magento upgrade.
Magento have a feature to keep our custom code in local(app/code/local) folder. Inside that folder we can keep our custom codes safely.
There are 2 way to override magento core module.
  1. Create same folder structure in local folder as like in core.
    Ex: If we want to override the Account controller in app/code/core/Mage/Customer/controllers/AccountController.php we can do by just creating same folder structure in local folder like app/code/local/Mage/Customer/controllers/AccountController.php. Here Account Controller have overridden by the file in local directory.
  2. Another way is to override controller using our own module.
    At first, i was frustrated because i was followed 4 more tutorials but couldn't able to override core controller. Finally got the solution from webspeaks.com that how to override controllers in magento

Follow the steps correctly, its case-sensitive so keep your eye closely.

Creating your own module

  1. Create Ws_Customer.xml file inside app/etc/modules/
  2. Create Ws(namespace) folder inside app/code/local/
  3. Create Customer(module) folder inside app/code/local/Ws
  4. Create config.xml file inside app/code/local/Ws/Customer/etc/
  5. Create AccountController.php file inside app/code/local/Ws/Customer/controllers/
Step:1Ws_Customer.xml -Enabling custom module.

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

step:2config.xml - Configuration for our module

<?xml version="1.0"?>
<config>
<modules>
<Ws_Customer>
<version>0.0.1</version>
</Ws_Customer>
</modules>
<frontend>
<routers>
<customer><!-- Name of core module to be overridden-->
<args>
<modules>
<Ws_Customer before="Mage_Customer">Ws_Customer</Ws_Customer><!-- Tell Magento to call our custom module before the Mage/Checkout module -->
</modules>
</args>
</customer>
</routers>
</frontend>
</config>

Step:3AccountController.php

require_once("Mage/Customer/controllers/AccountController.php");
class Ws_Customer_AccountController extends Mage_CUstomer_AccountController{
//Add your stuffs here
public function indexAction(){
echo 'Index action have been overriden!!';
parent::indexAction();
}
}

After completing, don't forget to clear Magento cache. Make sure that your module is in active [admin->system->configuration->Advanced->Advanced]
Is this post useful? Please comment on this!

Comments

  1. We have followed the override the controller in the app/code/local/Mage/Customer/controllers/AccountController.php ,but still its not working .

    ReplyDelete

Post a Comment

Popular posts from this blog

Send email from xampp localhost

Modify item price in cart after placing order using sales_quote_add_item

Convert long url to short url using tinyurl api