Create a custom router in Magento using Event/Observer

Have you ever wanted to create a seo-friendly url management feature for a custom module like News module or blog Module in Magento.
Here we are going to create a custom router as done by the CMS module to route a url to a specific page created in admin.

A little background into Magento Routers.
Magento by default has 4 routers which are Admin, Standard, Cms and Default respectively.
In short, during a page load, Magento loads the Mage_Core_Controller_Varien_Action class. This class is responsible for finding the routers that matches with the current request url and identify the controller and action to be called.

While loading the routers, “controller_front_init_routers” event is fired.

We can use this event to add our custom router feature into Magento.

So, Here we go.

Step 1.
Setup an observer in config.xml file of your module that listens to the “controller_front_init_routers” event.

Step 2.
Create a event listener class that actually does the job of adding a new router and also matches the url request to the required content in our module. For ease of coding, we will be using the cms class “Mage_Cms_Controller_Router” as the Magento’s core CMS module uses the same appraoch. We will make the changes as required.
So create a new file Router.php at app/code/codePool/CompanyName/ModuleName/Controller/ which is an exact copy of “app/code/core/Mage/Cms/Controller/Router.php”
and put the following code.

Read the comment in the code to make the necessary changes.

class CompanyName_ModuleName_Controller_Router extends Mage_Core_Controller_Varien_Router_Abstract
* Initialize Controller Router
* @param Varien_Event_Observer $observer
public function initControllerRouters($observer)
/* @var $front Mage_Core_Controller_Varien_Front */
$front = $observer->getEvent()->getFront();

$front->addRouter(‘router_name’, $this);//replace router_name with a suitable name

* Validate and Match router with the Page and modify request
* @param Zend_Controller_Request_Http $request
* @return bool
public function match(Zend_Controller_Request_Http $request)
if (!Mage::isInstalled()) {

$identifier = trim($request->getPathInfo(), ‘/’);

$condition = new Varien_Object(array(
‘identifier’ => $identifier,
‘continue’ => true
Mage::dispatchEvent(‘magentotraining_controller_router_match_before’, array(
‘router’ => $this,
‘condition’ => $condition

$identifier = $condition->getIdentifier();

if ($condition->getRedirectUrl()) {
return true;

if (!$condition->getContinue()) {
return false;

/** this is the actual implementation in CMS module to find the page ID from the request url, we can write something similar to **get the id of our item from the requested URL.
* for eg. we could have url = ‘about-us’ for id 1.
* So if the current url is http://site_url/about-us
* we will get 1 as pageId by quering through our module.

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

$pageId = 1; //1 is retrieved by quering our custom module table.
$request->setModuleName(‘modulename’)// replace modulename with the frontname of the router of your controller
->setControllerName(‘index’)// replace index with your controller name
->setActionName(‘index’) // replace index with your action name
->setParam(‘page_id’, $pageId); //$pageid is the id param that is passed into the action to retrieve the desired item


return true;

And we’re done.

Now when Magento processes the current request it also passes through our module to check if it finds a match and set the controller and action of our module for processing the request.
So, now we have a clean and elegant url for our item pages.
instead of
Looks good 😀

For more detail and intensive study on Magento routers and front Action Processing, you can have a look at the following posts:

Thank you for reading.
Hope this was helpful to you.


7 thoughts on “Create a custom router in Magento using Event/Observer

  1. I am generating dynamic url’s ends with ‘-s.html’. will the above code works for this url’s or what should be the code change for this url’s to work

    example urls

    • Hello Ravi,
      You need to code some mechanism to recognize argument corresponding to aa/bbb-s.html
      like the cms module does for converting url like about-us to corresponding page id.

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

  2. This pages not created using cms(Your code perfectly works for cms pages).this urls created from the third party and this pages doesn’t have any pageid. i did below changes it works when ‘aa’ or ‘cc’ not a category name or product name

    public function match(Zend_Controller_Request_Http $request)
    if (strpos($request->getRequestUri(), ‘-s.html’) !== false) {
    return true;

    • Thats good.
      You have to implement some logic in your code to handle the urls. If you don’t have page_ids then you must have some kind of logic behind what content the specific url is supposed to retrieve.

      • I am actually beginner to magento i dont have much knokledge could you please help me to solve this issues.

        1. when this kind of urls comes it should come to my controller(From this controllers i will display products)

      • Hello Ravi,
        ->setControllerName(“index”)// replace index with your controller name
        ->setActionName(“index”) // replace index with your action name
        ->setParam(“someparameter”, “your_parameter_value”);

        These lines in the article make it possible for the request to be routed to your controller

        If you know how to build a custom module in Magento, then it should take you to “IndexController”‘s “IndexAction” with “someparameter” as request parameters.

  3. The above condition is working fine for the url’s like something is not category name in my DB) but for the url’s like not working bcs here outdoor is a category name. when this kind of url’s comes my module not considered for the execution bcs magento trying to get category based routing.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s