Greeting! It’s time to share our knowledge on 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?
Yes, if you look to code, that means – Magento will use the standard router for processing requests and URLs, and when you follow magento.dev/test-frontname 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.HTML Code:<routers> <atwixtest> <use>standard</use> <args> <module>Atwix_Test</module> <frontName>test-frontname</frontName> </args> </atwixtest> </routers>
Now, let’s start creating own router. First of all, we need to declare router in config.xml:
After this, we should create file that will contain class Atwix_Test_Controller_Router app/code/local/Atwix/Test/Controller/Router.php:HTML Code:<default> <web> <routers> <atwixtest> <area>frontend</area> <class>Atwix_Test_Controller_Router</class> </atwixtest> </routers> </web> </default>
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.PHP Code:
class Atwix_Test_Controller_Router extends Mage_Core_Controller_Varien_Router_Standard
{
public function match(Zend_Controller_Request_Http $request)
{
$request->setModuleName('test-frontname')
->setControllerName('index')
->setActionName('index');
return true;
}
}
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. We are waiting for your comments and happy, non-crappy coding!View more threads in the same category:
- Prevent an array filter warning while adding products
- Best Development Practices in Magento 2
- How to Secure Magento Against SQL Injections
- How To Backup And Rollback In Magento 2
- Implement Your Custom API In Magento Backend
- How to Create Simple Twitter Feed Module in Magento 2
- How To Secure cron.php In Magento 2
- How To Create & Configure Multi Store In Magento 2
- Create Your Own Widget In Magento
- Third Part of Magento Theme Development
Bookmarks