Routing is a fundamental part of all MVC frameworks. In order to understand how Magento2 request routing works, it is essential to get familiar with execution flow that leads to certain request getting (or not getting) dispatched.

Routing is the process of determining the current request and validating the request in the router list. After installing front controller and matching a router in the database, Magento should be able to match the URL to a module controller and action name.

Magento 2  Request Routing
  • Route determination starts the search for the current route
  • Routing process handled by Front Controller and determined in the system
  • lib/internal/Magento/Framework/App/FrontController.php
  • The “Dispatch” function is built for the routing process in the front controller
Request Process Flow

router

Routing Algorithm

 

Explanation:
    • Front controller, iterates over available routers to find the current request using match-logic. The request route should be dispatched a maximum of 100 iterations or else its throw an exception. 
    • RouterList needs to be involved in the routing process. This object is injected from Magento/Framework/App/RouterList. 
    • Once the current request matches the router list, set the dispatch action as true and also get the response from the “setNoCacheHeader”. 
    • If the current request does not match the router list, it should throw an exception and the router needs to be forwarded  to  “noroute” action, i.e. redirected to 404 page.  
Structure of Router List:
  • Magento 2 is a dependency injection system. Each and every module is allowed to do RouterList. 
  • For Example: Magento_Store module supplies the Base and DefaultRouter
  • Routers are listed based on the sort order item, preferably in ascending order. The Base router always comes before the DefaultRouter.
  • In default Magento 2 installation, the DefaultRouter has the lowest priority. It will be the last to be validated for matches in the Front Controller. The DefaultRouter is always redirected to 404 page.
  • Once the Base router is processed, it will split into Module, Controller and Action, following which, action will be dispatched and performed with the current request.

Request routing in Magento 2 is a vast topic to dive-in and explore. Hope this blog serves as an introduction to Request Routing.

Useful Links

Be the first to Know

Subscribe to our blog to get the latest articles directly to your inbox.

READY TO DRIVE SALES?

Request a Quote Today

Request a Quote

Leave a Reply

2 Comments on "Magento 2 Request Routing"


Guest
lalit mohan
11 months 18 days ago

Awesome article Thanks!

admin
Admin
11 months 17 days ago

Hi Lalit,
Thanks so much for your appreciation. Please continue to read our blogs.

Subscribe to our blogs to know the latest in Magento.
Thanks again.