Magento – Customizable router frontName

configurationcontrollersrouter

I have a router like this

<routers>
    <mymodule>
        <use>standard</use>
        <args>
            <module>NSP_MyModule</module>
            <frontName>mymodule</frontName>
        </args>
    </mymodule>     
</routers>

which (in my environment) will be accessable with this URL /magento/mymodule
but I want the URL key mymodule to be custamizable through backend. How can I achieve that?

Best Answer

In this excellent post by Alan Storm, I found a hint to a possible solution. You should register your own router. An example can be found in Magento's CMS module.

In the CMS module's config.xml find this section:

<events>
    <controller_front_init_routers>
        <observers>
            <cms>
                <class>Mage_Cms_Controller_Router</class>
                <method>initControllerRouters</method>
            </cms>
        </observers>
    </controller_front_init_routers>
</events>

The method initControllerRouters() registers a new router:

public function initControllerRouters($observer)
{
    /* @var $front Mage_Core_Controller_Varien_Front */
    $front = $observer->getEvent()->getFront();

    $front->addRouter('cms', $this);
}

...that will be iterated in app/code/core/Mage/Core/Controller/Varien/Front.php:

while (!$request->isDispatched() && $i++<100) {
    foreach ($this->_routers as $router) {
        if ($router->match($this->getRequest())) {
            break;
        }
    }
}

The match() method of the CMS-Router contains this code:

$page   = Mage::getModel('cms/page');
$pageId = $page->checkIdentifier($identifier, Mage::app()->getStore()->getId());
if (!$pageId) {
    return false;
}

$request->setModuleName('cms')
    ->setControllerName('page')
    ->setActionName('view')
    ->setParam('page_id', $pageId);

...which leads to the file Cms/controllers/PageController.php. The class inside is named Mage_Cms_PageController and has a method viewAction() that asks for the request's parameter page_id. From here, you can look further at the method Mage::helper('cms/page')->renderPage($this, $pageId).

I think another big problem will be, if the configured frontName matches that of a system frontName, or the frontName defined by another module. I have no idea how this might work out.

Related Topic