Get Free Audit

Request Routing Flow in Magento 2

May 15, 2018

2277 Aleksei Grigorenko

Request Routing Flow in Magento 2

Today we are going to talk about request routing and take a look at its customization process. Additionally, we will show you when it is necessary to create new routers and to customize the existing ones. Let’s get these things under the way.

What is request routing flow in Magento 2?

Routing in Magento is a process, that matches a request received in Magento, and the call of the corresponding control module. First of all, an application is created in the index.php file. Here starts the process of determining which of the routers will be called. Below you can see the parts of the code that are called.

If you look at what the “dispatch” method contains, it becomes clear how the request is processed and the router is selected. Let us take a look at this code.

Here each of the routers ($this ➔ _routerList) is checked for compliance with the query. If found, the \”Magento\Framework\App\Action” object will be returned.

If none of the routers finds a matching query, the standard noroute action class is called.

It is easy to notice that the list of routers ($this _routerList) is processed in the cycle. By default this list consists of standard routers implementing RouterInterface, which are called in the following sequence:

  1. Base Router
  2. CMS Router
  3. UrlRewrite Router
  4. Default Router

Customize request routing

Magento allows you to add your router to this list. Now let’s consider an example of creating a custom router.

The registration of a new router takes place in di.xml. Here you can define the class that is responsible for processing the request (item name = “class”), the priority of calling the router (item name = “sortOrder”)

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

The request will be processed directly in the Router class.

You can check out the full version of the example of adding a new router here.

When it is necessary to create new routers and to customize the existing ones

Sometimes you may need to create your own router or customize an existing one. One good example is the “blog” module, when you need to redirect seo-friendly URLs to the corresponding posts. For this purpose, the module creates its own Router, which distributes the queries.

Also, when migrating from any other platform to Magento, you may need to retain the connection between the old URLs and the new addresses. To do this, you can also use an additional router, which will contain the logic of finding a matching URL (old_webshop_url – magento_webshop).

Customization of an existing router is required, when there is no way to bypass the processing of a standard router request. For example, the router is run in the queue earlier than the custom one and doesn’t find the path required for customization.

Check out a related article to find out more about request routing in Magento 2.

Vlad Yunusov
Partner With Us Looking for a partner that will help you to grow your business? We are the right company to develop your webstore. Feel free to get in touch with us. We will be happy to discuss your business opportunities and provide you with a Free Quote. Talk to Vlad

Post a new comment

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