Routing in Magento has a significant role that depends on the processing of URL requests. Router classes that are responsible for matching and processing the Magento HTTP Request. In this tutorial, we will cover the flow of the Magento 2 routes and analyses some of the default routers, that will explain how routers match requested actions.
Request Flow
STEP 1
Magento 2.0 Request starts with Index.php file.
Magento has two index.php files
- /var/www/magento2/index.php – Development,
- /var/www/magento2/pub/index.php – Production.
STEP 2
Objectmanagerfactory and bootstrap objects are initialized in Index.php
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
STEP 3
After initiating the objectmanagerfactory and bootstrap, Objectmanager and application will be created and return back to index.php.
$app = $bootstrap->createApplication(‘Magento\Framework\App\Http’);
STEP 4
FrontController returns the ResultInterface to the Application Interface that renders the output.
STEP 5
HTTP Application instance initializes routing. It takes the first part of the URL to determine whether the request is from the frontend or backend.
After determining the area of request, Magento2 load the related configuration (unlike Magento 1 which loads all the configuration). Application object calls Magento\Framework\App\FrontControllerInterface::dispatch method of requested area.
STEP 6
Magento 2.0 routing process is same as the Magento 1. All the routers are iterated to match request.
If the requested action is found, its action controller instance returns. When front controller gets the action controller instance it calls the dispatch method. This routing process is more secure because we can’t access the admin request in front-end or front-end request in back-end.
STEP 7
Dispatch method calls the action method of requested action, Action controller execute the all task and return the instance of some implementation of ResultInterface.
STEP 8
FrontController returns the ResultInterface to the Application Interface that renders the output.
Now we know about the Magento2 request flow, Now we can move forward to see how exactly the router matching works to what are routers.
What is Router?
Router is the PHP class which is responsible for matching and processing the magento URL requests.
Every Magento HTTP request uses the Router::match() method, If it returns false then we continue to the next routes but if it returns true then we dispatch the controller action.
Magento 2 Comes with default routers like Base, CMS, UrlRewrite, and DefaultRouter.
The default flow of the above routers is
Base Router → CMS Router → UrlRewrite Router → Default Router
Let’s see each router one by one.
- Base Router/Standard Router:
Base Router or Magento 1 developer may refer to as the Standard Router is located at lib/internal/Magento/Framework/App/Router/Base.php
Magento 2 First matches the action request to this router and its match methods are matchAction and parseRequest, and the other one will set the controller name path, module frontname, action name, controller module and route name etc. Base router matches the standard base url(front name/action path/action/param1/etc params/) of magento 2.0.
- CMS Router:
CMS Router located at /vendor/magento/module-cms/Controller/Router.php, CMS Router is used for handling the CMS Pages and it also set the module name to “cms”,controller name to “page” and action name to “view” so that make the full path to execute is /vendor/magento/module-cms/Controller/Page/View.php controller. After setting the base router format it will set the page id and forward it but won’t dispatch it. By Forwarding, it will break the current loop and start new loop again (Max 100 times it will do that). so new loop will match the url and activate the view controller in module-cms/Controller/Page and show the saved page ID. it will find the page ID depending on the url.
- UrlRewrite Router
Magento 2 urlRewrite has it’s own router, If you are familiar with magento 1 then you know that the URL rewrite is the part of base router/Standard router process in magento 1.
URLRewrite router located at /vendor/magento/module-url-rewrite/Controller/Router.php, it uses the URL finder to get the URL -rewrite that matches URL from the database.
It will forward action just like the CMS router.
- Default Router
Default Router is located at lib/internal/Magento/Framework/App/Router/DefaultRouter.php. It’s last in the loop and used when above 3 routers can’t process the request.
Summary
After reading this article, Your concept of default routing in Magento should be cleared. You’ll see Custom Routers in Magneto in upcoming articles.
At Elitech, We have an expert web development team of exceptionally talented, trained and well experienced front end developers and PHP Programmers who have strongly contributed in producing outstanding custom Magento eCommerce web applications.
We provide custom Magento development services including theme customization, payment gateway integration, and Magento2 latest version up-gradation. Our developer’s strong technical & domain expertise can contribute immensely to grow your business online.
Comments are closed.