Magento – Overriding controller

controllersmagento-1.9overrides

I have followed few instructions found in this forum, but I must be missing something. I am trying to override Contacts controller, specifically postAction method. Here is what I have got:

Magento version: 1.9.3.1;
Namespace: MageOverload;
Module: ContactsModule;

/app/code/local/MageOverload/ContactsModule/etc/config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <MageOverload_ContactsModule>
            <version>1.0.0</version>
        </MageOverload_ContactsModule>
    </modules>
    <frontend>
        <routers>
            <contactsmodule>
                <args>
                    <modules>
                        <MageOverload_ContactsModule before="Mage_Contacts">MageOverload_ContactsModule</MageOverload_ContactsModule>
                    </modules>
                </args>
            </contactsmodule>
        </routers>
    </frontend>
</config>

/app/etc/modules/MageOverload_ContactsModule.xml:

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

/app/code/local/MageOverload/ContactsModule/controllers/IndexController.php:

<?php
require_once Mage::getModuleDir('controllers', 'Mage_Contacts') . DS . 'IndexController.php';
class MageOverload_ContactsModule_IndexController extends Mage_Contacts_IndexController
{
    public function postAction() {
        //... custom code
    }
}

I have flushed cache, custom module is visible via magento admin panel, but still the system is using Mage core files for Contacts. Any help would be greatly appreciated.

Best Answer

Your controller rewrite XML part should look like this:

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

As you can see instead of using <contactsmodule> node we are here using <contacts> node. <contacts> is the router node which is using by Mage_Contacts module.

After this change is done, do not forget to flush the cache again.

Free Tip:

It is a good practice to keep dependency in your module as shown below.

File : MageOverload_ContactsModule.xml

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

This clearly indicates that, your module has a dependency on Mage_Contacts module and thus Magento will load your module only after Mage_Contacts is loaded.

Related Topic