Skip to main content
3 of 3
added 10 characters in body
7ochem
  • 7.6k
  • 14
  • 52
  • 80

If you want to load your custom router prior to all other routers in Magento, then try this one.

Define your custom router in config.xml

File : app\code\{codePool}\{Namespace}\{Module}\etc\config.xml

<config>
    <default>
        <web>
            <routers>
                <{router_identifier}>
                    <area>{admin|frontend}</area>
                    <class>Namespace_Module_Controller_Router</class>
                </{router_identifier}>
            </routers>
        </web>
    </default>
    <stores>
        <default>
            <web>
                <routers>
                    <{router_identifier}>
                        <area>{admin|frontend}</area>
                        <class>Namespace_Module_Controller_Router</class>
                    </{router_identifier}>
                </routers>
            </web>
        </default>
    </stores>
</config>

Now make core module depend on your module

File : app\etc\modules\Namespace_Module.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Namespace_Module>
            <active>true</active>
            <codePool>{local|community}</codePool>
        </Namespace_Module>
        <Mage_Core>
            <depends>
                <Namespace_Module />
            </depends>
        </Mage_Core>
    </modules>
</config>

Now clear your cache. You are done.

Why does This work

Router loading is happening inside front-controller1. The abbreviated form of router loading code is shown below.

public function init()
{

    //prioriy 1 : routers adding via xml configuration
    $routersInfo = Mage::app()->getStore()->getConfig(self::XML_STORE_ROUTERS_PATH);
    foreach ($routersInfo as $routerCode => $routerInfo) {
        if (isset($routerInfo['disabled']) && $routerInfo['disabled']) {
            continue;
        }
        if (isset($routerInfo['class'])) {
            $router = new $routerInfo['class'];
            if (isset($routerInfo['area'])) {
                $router->collectRoutes($routerInfo['area'], $routerCode);
            }
            $this->addRouter($routerCode, $router);
        }
    }

    //priority 2: routers adding via observer
    Mage::dispatchEvent('controller_front_init_routers', array('front'=>$this));

    // pririry 3 : for default router.
    $default = new Mage_Core_Controller_Varien_Router_Default();
    $this->addRouter('default', $default);

    return $this;
}

This method simply grabs routers from various resources and add it to the protected property $_routers as an array2. As you can see, the priorities are like this:

  1. Routers which are defined via XML files will be added to $_routers first

  2. Routers which are added via the event controller_front_init_routers go next

  3. At last, the Default router will be added to the array

Now, the standard router and admin router are defined in app/code/core/Mage/Core/etc/config.xml and hence they will be loaded first in the $_routers array and thus they will be processed first.

In order to change this intentional priority for the standard and admin router, you need to define your router via your module's config.xml file. This will bring your router to the priority level 1. Making core module depend on your module gives more priority to your module and hence Magento will consider your module first and hence load your router in $_routers array first.

Hope that makes sense.


1: Mage_Core_Controller_Varien_Front::init()

2: $this->addRouter() is actually what populates $_routers.

Rajeev K Tomy
  • 17.3k
  • 6
  • 63
  • 103