Magento

How to override a controller in Magento

How to override a controller in Magento

In several time ,we need to override a controlller.How to override a controller in Magento.
There are two process available:
1.Basic Old process.
2.Upgrade Process from Magento CE 1.5.0.

First have describe second process. It available from Magento CE 1.5.0.Where, i have

Overriding controllers magento core controller

.I have using before tag for use to

override controller of a module-

 

Here ,step to override a controllers:
a)First check which module ,i want to override.

Ex1: Suppose have override magento Magento Core Module Mage_Contacts and want override IndexController.php
Write below code

<frontend>
		<routers>
			<contacts>
				<args>
					<modules>
						<customcontacts before="Mage_Contacts">Amit_Customcontacts</customcontacts>
					</modules>
				</args>
			</contacts>
		</routers>
	</frontend>

Here <contacts> is frontend routers for Mage_Contacts,
and In <customcontacts before=”Mage_Contacts”>Amit_Customcontacts</customcontacts> , <customcontacts is a unique name this rewrite.
Amit_Customcontacts ,Amit is My module namespace ,and Customcontacts is Module name.
Location:app/code/local/Amit/Customcontacts/etc/config.xml and full code for this file

<?xml version="1.0"?>
<config>
	<modules>
	<Amit_Customcontacts>
			<version>1.0.0</version>
		</Amit_Customcontacts>
	</modules>
	<frontend>
		<routers>
			<contacts>
				<args>
					<modules>
						<customcontacts before="Mage_Contacts">Amit_Customcontacts</customcontacts>
					</modules>
				</args>
			</contacts>
		</routers>
	</frontend>	

</config>

b)Now need to define Controller file, Which name should be same name Overriding Controller. If override Customer Module AccountController.php then it overridden Controller name should be AccountController.php. Also need to include parent class file and the class.
Ex: As per as, override IndexController.php then overridden file is IndexController.php

<?php 
require_once Mage::getModuleDir('controllers', "Mage_Contacts").DS."IndexController.php";
class Amit_Customcontacts_IndexController extends Mage_Contacts_IndexController
{
	public function indexAction(){
		parent::indexAction();
	}
}

As contacts us routers is contacts/index/index that means IndexController.php and index Action and IndexController.php path is Mage/Contacts/controllers then Override controller(IndexController.php) path is
app/code/local/Amit/Customcontacts/controllers /

<?php 
require_once Mage::getModuleDir('controllers',"Mage_Contacts").DS."IndexController.php";
class Amit_Customcontacts_IndexController extends Mage_Contacts_IndexController
{
	public function indexAction(){
		parent::indexAction();
	}
}

c)Add the following to use the same update handle as before:
I have layout file that the file to change is app/design/frontend/[myinterface]/[mytheme]/layout/mymodule.xml

Add the following lines:

    <customcontacts_contacts_index_index>
        <update handle="contacts_index_index"/>           
    </customcontacts_contacts_index_index>

Second Procedure:

Another process is basic magento process. Where overload is contacts controllers
Where customcontacts routers to contacts

1.define my module routers

 <frontend>
        <routers>
            <customcontacts>
                <!-- should be set to "admin" when overloading admin stuff (?) -->
                <use>standard</use>
                <args>
                    <module>Amit_Customcontacts</module>
                    <!-- This is used when "catching" the rewrite above -->
                    <frontName>customcontacts</frontName>
                </args>
            </customcontacts>
        </routers>
    </frontend>

b)rewrite rule could be added to the database instead

  <global>
        <!-- This rewrite rule could be added to the database instead -->
        <rewrite>
            <!-- This is an identifier for your rewrite that should be unique -->
            <!-- THIS IS THE CLASSNAME IN YOUR OWN CONTROLLER -->
            <customcontactsunique>
                <from><![CDATA[#^/contacts/index/#]]></from>
                <!--
                    - mymodule matches the router frontname below
                    -  matches the path to your controller

                    Considering the router below, "/customcontacts/index/" will be
                    "translated" to "app/code/local/Amit/Customcontacts/controllers/IndexController.php" (?)
                -->
                <to>/customcontacts/index/</to>
            </customcontactsunique>
        </rewrite>
    </global>

Where customcontactsunique is rewrite identifier code which should be unique.

Here picture of Basic procedure

override_controller
How to override a controller in Magento

Rest of Indexcontrollers.php file code is same as above.

Most Import:Also,need to module file Amit_Customcontacts.xmlwhich is located at app/etc/modules .it importance both Process

 <?xml version="1.0"?>
    <config>
        <modules>
            <Amit_Customcontacts>
                <active>true</active>
                <codePool>local</codePool>
               <depends><Mage_Contacts/></depends>
            </Amit_Customcontacts>
        </modules>
    </config>