Magento: How to override a controller easily

In seeking how to override a controller in Magento, you find lots of different ways to proceed. Some work, some do not. I’ll show you how to override a controller very easily.

Override an admin controller

Changing the config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Namespace_Adminhtml>
            <version>0.1.0</version>
        </Namespace_Adminhtml>
    </modules>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <Namespace_Adminhtml before="Mage_Adminhtml">Namespace_Adminhtml</Namespace_Adminhtml>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
</config>

Override the controller

Create file:

app/code/local/Namespace/Adminhtml/controllers/Cms/PageController.php
<?php
 
require_once 'Mage/Adminhtml/controllers/Cms/PageController.php';
 
class Namespace_Adminhtml_Cms_PageController extends Mage_Adminhtml_Cms_PageController
{
    public function indexAction()
    {
        // My stuff
    }
}

Override a front controller

Changing the config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Namespace_Cms>
            <version>0.1.0</version>
        </Namespace_Cms>
    </modules>
    <frontend>
        <routers>
            <cms>
                <args>
                    <modules>
                        <Namespace_Cms before="Mage_Cms">Namespace_Cms</Namespace_Cms>
                    </modules>
                </args>
            </cms>
        </routers>
    </frontend>
</config>

Override the controller

Create file:

app/code/local/Namespace/Cms/controllers/IndexController.php
<?php
 
require_once 'Mage/Cms/controllers/IndexController.php';
 
class Namespace_Cms_IndexController extends Mage_Cms_IndexController
{
    public function indexAction()
    {
        // My stuff
    }
}

10 Responses to “Magento: How to override a controller easily”

  1. Raja says:

    hye, dear Johann it is not working for me, i am using magento 1.6.2.0, and nothing happens same core controller is called. can u please help?

  2. manik says:

    Hey Johann,
    I’ve created a custom module manik_ajax, it shows active in the modules list but I’m not able to override CartController.php, it is weird because it works perfectly in my local machine (Mac OSX) but doesn’t in my Server (Linux, Ubuntu), I’ve found out that mostly its because of case issues but cases seem alright to me, please help me:
    app/code/local/manik/ajax/controllers/Checkout/CartController.php – It is the copy of original file except an echo ‘hello world'; command in index action
    app/code/local/manik/ajax/etc/config.xml

    0.0.1

    manik_ajax_Checkout

  3. manik says:

    Since the xml was omitted here’s the xml code http://tinypaste.com/2c0aa0df

  4. Robin says:

    Hi,

    thanks for the article.Let me clear one doubt if my custom module have the same name of the module which am going to override what will happen?

  5. Roman says:

    Thank you. You saved a lot of time for me.

  6. Ravi says:

    I have to override local module controller method by local module. how can i?

  7. Andrey says:

    This is completly useless!!!
    For a person that is trying to learn magento this is so confusing.
    For example:
    First config.xml use this namespace “” then in second the namespace change to this one “”.
    No explanations about this happens.
    No folders where to put this files.

    I come here looking for a solution, but now Im more confuse.

  8. Victor says:

    Works like a charm. Thanks!

Leave a Reply

* Required fields

Categories