How to Create Custom Router in Magento 2

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:

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

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.



Post a new comment

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