Magento – How to Change Router Load Order

controllersmagento-1router

I have a custom router that I'd like to be used for matching before the Standard router. I'm having issues trying to figure out how to change the order in which the routers are added and used for matching requests subsequently.

It looks like the Admin and Standard routers are defined in Mage_Core's config.xml and loaded in Mage_Core_Controller_Varien_Front::init with following line.

$routersInfo = Mage::app()->getStore()->getConfig(self::XML_STORE_ROUTERS_PATH);

However, I've tried to add my custom router's definition in Mage_Core's config.xml before any of the default routers to test, but it still gets added at the end. I've tried changing the order in which Admin and Standard routers are defined in config.xml, and that doesn't take effect. I clear cache, and I'm not sure why.

I wan to do this without rewriting Mage_Core_Controller_Varien_Router_Standard (app/code/local/Mage/Core/...), if possible. Any ideas?

Best Answer

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.

Related Topic