Controllers, Routers and Responses in Magento 2

Apr 12, 20183662
Controllers, Routers and Responses in Magento 2

We continue to explore the latest version of the most popular ecommerce platform and today we are going to learn about Request routing in Magento 2, namely Front controller, Controllers, Routers and Responses. Technical extracts are included in the article, as usual, to make it more practical. Two more Magento 2 How-to’s are going to be published next week.

So let’s get down to work.

Front Controller

Routing in Magento 2 is based on Front Controller Pattern. Front Controller is a design pattern, in which one component is responsible for processing all the incoming inquiries, their redirection to the corresponding components, further processing of the results and returning the results back to the browser.

In Magento 2, this path starts from creating the front controller instance and subsequent calling \Magento\Framework\App\FrontControllerInterface::dispatch method in \Magento\Framework\App\Http::launch method.

If the required path is found successfully, FrontController iterates through the available routers call the method \Magento\Framework\App\Action\AbstractAction::dispatch of the corresponding controller.

Routers

All routers in Magento 2 should implement \Magento\Framework\App\RouterInterface interface and define \Magento\Framework\App\RouterInterface::match method. This method is responsible for matching and processing URL inquiries. In case of a successful match the router returns the corresponding controller instance. If the necessary controller is not found, Front Controller moves on to the next router.

Magento 2 comes with 4 routers:

  1. Base Router (\Magento\Framework\App\Router\Base) sets module front name, controller and action names, controller module and route name if found. Processes standard Magento URLs.
  2. CMS Router (\Magento\Cms\Controller\Router) is used for processing CMS pages. Sets module name to “cms”, controller name to “page”, action name to “view” and page id depending on the requested page. After that it forwards request but won’t dispatch it. This will lead to Front Controller starting the next cycle of router checking, on which Base Router based on the established path will call \Magento\Cms\Controller\Page\View, which will show the necessary page.
  3. UrlRewrite Router (\Magento\UrlRewrite\Controller\Router) is responsible for catalog URL rewrites. It uses Url Finder to find the necessary URL in the database, and after that returns the forward just like CMS Router.
  4. Default Router (\Magento\Framework\App\Router\DefaultRouter) is used when no other router is able to find the appropriate controller. It’s responsible for 404 Page.  

Controllers

Controllers in Magento 2 differ from typical controllers in MVC applications. Magento 2 controllers are responsible for only one specific URL and contain only one execute method. This method is responsible for returning result object and occasional processing of input POST data. All controllers inherit \Magento\Framework\App\Action\Action class.

The required controller is searched in the Base Router, and then it’s called in the Front Controller.

\Magento\Framework\App\Router\Base::parseRequest method is responsible for splitting the requested URL into segments, and \Magento\Framework\App\Router\Base::matchAction method tries to find a suitable controller based on the received segments.

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

Each segment in URL contains information for finding the necessary controller. Segments in URL can be represented as follows: moduleFrontName/actionPath/actionName, where:

  • moduleFrontName — modules front name as set in routes.xml file;
  • actionPath — the path to the controller;
  • actionName — the name of the controller.

For URL like custom-module/info/product the path to the controller class might look like this: app/code/Vendor/Custom_Module/Controllers/Info/ProductController.php.

magento development services

Magento Development Services

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

Click to visit the page

Responses

The controller in Magento 2 can return several response types depending on the purpose and the necessary result.

  • Page Result (\Magento\Framework\View\Result\Page) is the most common type of response. By returning this object, the controller starts the standard page rendering based on the corresponding XML layout handle.

  • JSON Result (\Magento\Framework\Controller\Result\Json) allows you to return a response in JSON format. It can be used in API or AJAX requests.

  • Raw Result (\Magento\Framework\Controller\Result\Raw) is used if you want to return a string to the browser without using Magento layout and view rendering.

  • Forward Result (\Magento\Framework\Controller\Result\Forward) allows to call another method/controller without changing the URL or redirecting.

  • Redirect Result (\Magento\Framework\Controller\Result\Redirect) is used when a user needs to be redirected to a different URL.

We hope the article was helpful and led to better understanding of Request routing in Magento 2.

Need expert Magento 2 development for your online store? Turn to BelVG.


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

5 Comments

  1. Nice Article…Now, with Magento 2.3 below routers are available for front-end area:
    1. Robots
    2. Urlrewrite
    3. Standard
    4. Cms
    5. Default

    Thanks for posting…

  2. I think the controller class path in your example (“For URL like custom-module/info/product the path to the controller class might look like this: app/code/Vendor/Custom_Module/Controllers/Info/ProductController.php) is wrong; in Magento 2 the correct path should be app/code/Vendor/Custom_Module/Controller/Info/Product.php

  3. Hi! Great thanks for your comment! You are right, and I will take your note into account in the future.

Post a new comment

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