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:
Routers which are defined via XML files will be added to $_routers
first
Routers which are added via the event controller_front_init_routers
go next
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
.
Mage_Core
module depend on your module?config/default/web/routers
inconfig.xml
, instead of theconfig/stores/default/web/routers
node thatMage_Core_Controller_Varien_Front
uses to add the routers. It seems like I can load mine first now. I didn't realize there was aconfig/*stores*/...
node.Mage/Core/etc/config.xml
, but it doesn't seem possible from a local extension. The only way for me is to comment outconfig/stores/...
note entirely inMage/Core/etc/config.xml
and redefine it in the order I want in my extension. Any idea how to do this without modifying core? For now, I just overrode the entire Standard router to skip matching for a certain request.