Get Free Audit

How to Create Custom Router in Magento 2

Apr 17, 2018

14178 Andrey Litvin

How to Create Custom Router in Magento 2

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:


magento development services

Magento Development Services

Take your online store to the next level with BelVG Magento development

Click to visit the page

After that we need to create a CustomRouter class.
app/code/Vendor/Module/Controller/CustomRouter.php:

Andrey_Dubina
Partner With Us Let's discuss how to grow your business. Get a Free Quote.
Talk to Andrey


And finally we need to create a routes.xml file.
app/code/Vendor/Module/etc/routes.xml:


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!

Andrey Dubina
Partner With Us Looking for a partner to grow your business? We are the right company to bring your webstore to success. Talk to Andrey

10 Comments

  1. Hi, Abid! Thanks for your comment.
    You offered a very valuable solution to your problem, and our team of developers greatly appreciates your contribution.

  2. 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.

  3. 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

  4. 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

  5. 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.

  6. 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.

  7. 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.

Post a new comment

BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *