We are going on with our Magento 2 tutorials and today we are reviewing the process of custom router creation in Magento 2. This is the second article out of the series devoted to Magento 2 how-to’s, be sure to check out last week’s Part 1, were we explored Controllers, Routers and Responses in Magento 2.
So, how do you create Custom routers in Magento2?
Custom Router
To create a router first you need to add it to the \Magento\Framework\App\RouterList, which is transferred to the Front Controller and contains all the available routers in the right order. To do this, we use the di.xml file in our module.
app/code/Vendor/Module/etc/di.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Framework\App\RouterList"> <arguments> <argument name="routerList" xsi:type="array"> <item name="customrouter" xsi:type="array"> <item name="class" xsi:type="string">Vendor\Module\Controller\CustomRouter</item> <item name="disable" xsi:type="boolean">false</item> <item name="sortOrder" xsi:type="string">22</item> </item> </argument> </arguments> </type> </config> |
Magento Development Services
Take your online store to the next level with BelVG Magento development
Click to visit the pageAfter that we need to create a CustomRouter class.
app/code/Vendor/Module/Controller/CustomRouter.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
namespace Vendor\Module\Controller; class CustomRouter implements \Magento\Framework\App\RouterInterface { protected $actionFactory; protected $_response; public function __construct( \Magento\Framework\App\ActionFactory $actionFactory, \Magento\Framework\App\ResponseInterface $response ) { $this->actionFactory = $actionFactory; $this->_response = $response; } public function match(\Magento\Framework\App\RequestInterface $request) { $identifier = trim($request->getPathInfo(), '/'); if(strpos($identifier, 'customrouter') !== false) { $request->setModuleName('customrouter')-> //module name setControllerName('index')-> //controller name setActionName('index')-> //action name setParam('param', 3); //custom parameters } else { return false; } return $this->actionFactory->create( 'Magento\Framework\App\Action\Forward', ['request' => $request] ); } } |
And finally we need to create a routes.xml file.
app/code/Vendor/Module/etc/routes.xml:
1 2 3 4 5 6 7 8 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd"> <router id="standard"> <route id="customrouter" frontName="customrouter"> <module name="Vendor_Module" /> </route> </router> </config> |
That was a brief overview of creating Custom Router in Magento 2. As I mentioned before, this is the 2nd part of the 3-part series devoted to Magento 2 tutorials. Part 3 is about to be published soon.
Looking for an experienced Magento 2 development team? Turn to BelVG!
Wonderful Blog.
Hi, Abid! Thanks for your comment.
You offered a very valuable solution to your problem, and our team of developers greatly appreciates your contribution.
Hi, Hafiz! I would advise you to turn to our support at [email protected] and our team of developers will provide you with expert assistance for your issue.
Sir my path is domain.com\router_id\controller\action .
i want to get this output on domain com mean landing page.
i use your Routing tutorial but is not work
After I used the above solution I was getting error:
Exception #0 (LogicException): Front controller reached 100 router match iterations
So I started reading the comments. I got the idea to change sort order. I changed it randomly to 99. And voila, it worked perfectly.
Note: I am testing on magento 2.1.17
Hello, Roman! Thanks for your questions.
Before I answer them, please bear in mind that the article was written a year ago, and was not tested at Magento 2.3 version.
1. In this case di.xml will apply both to frontend area and to adminhtml area.
2. devdocs.magento.com/guides/v2.3/extension-dev-guide/routing.html#custom-routers you can learn here that you should return the router as you make a forward.
3. Sort order of 22 means that this router will be processed BEFORE the standard router.
Thanks for the article, but it little bit confused me:
1. You define new router with name “customrouter” – don’t work; di.xml must be in etc/fontend or etc/adminhtml
2. In app/code/Vendor/Module/Controller/CustomRouter.php you change $request object, and make forward
3. Now standard router have to pick-up request, but it won’t, at least in magento2.3, because standard router sort order is 30 your new router is 22 -> it never fetch request.
Hi, Jonas. It’s great you appreciated my article, and thanks for the comment.
A custom router is often used for conducting requests to the standard Magento router, so, depending on the situation, we may need a smaller sortOrder than the standard one.
Thanks for the article – it helped me a lot with my custom router.
Just want to keep in mind that the standard router in Magneto 2.3 starts at sortorder 30. So 22 was not workin for me.
https://devdocs.magento.com/guides/v2.3/extension-dev-guide/routing.html
The information about the custom router have been discussed here very nicely.