A Complete Tutorial On Magento 2 Routing

A Complete Tutorial On Magento 2 Routing

Merry Christmas 🎅

Magento 2 routing defines a URL of the module.

The entire Magento 2 application flow is based on processing URL request and router classes which are responsible for matching and processing that requests.

The request URL in Magento 2 is http://example.com/index.php/router_name/controller/action

Here, the router_name is used to find the module.

When a request is made in Magento 2, controller/action: index.php → HTTP app → FrontController → Routing → Controller processing → etc flow is followed.

The FrontController is called in Http class to routing the request which will find the controller/action match.

vendor/magento/framework/App/FrontController.php

 

Steps to create a custom route on frontend and admin:

Prior to implementing the below method, you may refer to the tutorial on Magento 2 module development.

Create a frontend route:

Create routes.xml in app/code/[Vendor]/[Module]/etc/frontend Folder add the following code:

 

Create admin route:

Create routes.xml in app/code/[Vendor]/[Module]/etc/adminhtml folder and add the following code:

 

Use route to rewrite controller:

Create routes.xml in app/code/[Vendor]/[Module]/etc/frontend Folder and add the following code:

That’s all about Magento 2 Routing!

What are your opinions and experiences with routers in Magento 2? Don’t forget to share them in the comment section below.

In case you feel you need some extra help, feel free to get in touch through the comment section and I will be there to help!

Get Weekly Updates

Never miss Magento tips, tricks, tutorials, and news.

Thank you for subscribing.

Something went wrong.

5
(based on 8 Reviews)

Sanjay is a co-founder at Meetanshi. He is a Certified Magento Developer who loves creating Magento E-commerce solutions. Owing to his contributions in Magento Forums and posting solutions, he is among the top 50 contributors of the Magento community in 2019. When he is not engrossed with anything related to Magento, he loves to play cricket.

Leave a reply:

Your email address will not be published.