Wednesday , December 19 2018
Home >> All tutorial >> Creating custom router in Magento

Creating custom router in Magento

How to create custom router, as many of us use the standard router? You should know that Magento has four types of the routers: admin, standard, cms and default – they are loading in the order we’ve described. But, have you ever faced with such configuration?

  1. <routers>
  2.     <atwixtest>
  3.         <use>standard</use>
  4.         <args>
  5.             <module>Atwix_Test</module>
  6.             <frontName>testfrontname</frontName>
  7.         </args>
  8.     </atwixtest>
  9. </routers>
If you look to  <use>standard</use>  code, that means – Magento will use the standard router for processing requests and URLs,  and when you follow URL, in this case, Magento says for the standard router that it should take action indexAction from IndexController from module Atwix_Test – it is not so complicated, we guess. Actually, when the requests come to Magento, then it calls all routers till one of them will response – otherwise, there 404 page will be displayed.

Now, let’s start creating own router. First of all, we need to declare router in config.xml:

  1. <default>
  2.     <web>
  3.         <routers>
  4.             <atwixtest>
  5.                 <area>frontend</area>
  6.                 <class>Atwix_Test_Controller_Router</class>
  7.             </atwixtest>
  8.         </routers>
  9.     </web>
  10. </default>
After this, we should create file that will contain class  Atwix_Test_Controller_Router app/code/local/Atwix/Test/Controller/Router.php:
  1. class Atwix_Test_Controller_Router extends Mage_Core_Controller_Varien_Router_Standard
  2. {
  3.     public function match(Zend_Controller_Request_Http $request)
  4.     {
  5.         $request->setModuleName(‘test-frontname’)
  6.             ->setControllerName(‘index’)
  7.             ->setActionName(‘index’);
  9.         return true;
  10.     }
  12. }

The previous code explains Magento that it should transmit control to the indexAction function which is placed in app/code/local/Atwix/Test/controllers/IndexController.php., and your router will be called after admin and standard, but if these routers do not respond for the requests – then your router will be called.

So, now you can check request, proceed it and show page you want  before Magento will throw “Not found” page. Hope that simple tutorial will be useful.

Magento Tutorial by Atwix

Here is the library of Magento Download for FREE (all versions)

Your Magento website needs a powerful Magento ERP for stock control.


magento erp

About Champ

Leave a Reply

Your email address will not be published. Required fields are marked *

Heap | Mobile and Web Analytics