In order to achieve this, you need to manually add your dynamic routes to Standard-Router1 before dispatching of router happens. I will show you how you can achieve this with an example below. Using which, you can implement rest.
You need to listen to the event controller_front_init_routers
and add your routes in the observer.
File: app\code\{codePool}\{Namespace}\{Module}\etc\config.xml
<config>
<global>
<events>
<controller_front_init_routers>
<observers>
<add_dynamic_frontnames>
<class>namespace_module/observer</class>
<method>addDynamicFrontnames</method>
</add_dynamic_frontnames>
</observers>
</controller_front_init_routers>
</events>
</global>
</config>
Note : Important. Observer should be inside global scope.
File : app\code\{codePool}\{Namespace}\{Module}\Model\Observer.php
<?php
class Namespace_Module_Model_Observer
{
const DYNAMIC_FRONTNAMES = 'sytem/config/path/to/dynamic/routes';
public function addDynamicFrontnames(Varien_Event_Observer $observer)
{
//get front controller
$front = $observer->getEvent()->getFront();
//grab request object and standard router
$request = $front->getRequest();
$standardRouter = $front->getRouter('standard');
//grab module and route info using frontname;
//note:a router with frontname 'mymodule' should be configured in your module
$drouteName = $standardRouter->getRouteByFrontName('mymodule');
$dmoduleName = $standardRouter->getModuleByFrontName('mymodule');
//get dynamic frontnames from system config section
$_dynamicFrontNames = explode(',',
Mage::getStoreConfig()->getNode(self::DYNAMIC_FRONTNAMES)
);
//add your dynamic frontnames to standard router
foreach ($_dynamicFrontNames as $frontName) {
$standardRouter->addModule($frontName, $dmoduleName, $drouteName);
}
return $this;
}
What we are doing here is, we are using Front-Controller2 to grab Standard-Router. Then we will collect module-name and router-name using our frontName mymodule
. Then set the same module-name and router-name for every dynamic frontname which we collect from the system config section sytem/config/path/to/dynamic/routes
. So all the frontnames which you configured from system config section will just behave like mymodule
frontname here.
So along with these event setup, you are required to setup a router like this in your config.xml
file.
<config>
<frontend>
<routers>
<{router_identifer}>
<use>standard</use>
<args>
<module>Namspace_Module</module>
<frontName>mymodule</frontName>
</args>
</{router_identifer}>
</routers>
</frontend>
</config>
Also you need to setup a system.xml
file to configure your dynamic frontnames and replace the constant DYNAMIC_FRONTNAMES
with the the right path in the observer file above.
Hope that makes sense.
1:Mage_Core_Controller_Varien_Router_Standard
2:Mage_Core_Controller_Varien_Front