Magento 2 is a very powerful eCommerce platform that is highly extensible and customizable. But for the developers, it is very tricky to know that how the application is triggered?
When the Magento application is instantiated behind the scene Magento performs various sequences of operation. To understand these operations it’s important to understand the Magento request flow. So today we are going to explain about Magento Request Flow:
Step 1 – index.php
It’s is an entry point of the Magento 2 application. Here you will see the following code:
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
The method create() creates the instance of object manager factory.
In the very next line, this factory method creates the Application instance.
$app = $bootstrap->createApplication(\Magento\Framework\App\Http::class);
Once the application instance is created the Magento will run the application by calling the run() method as below:
$bootstrap->run($app);
Step 2 – Run()
If you are using the Xdebug or any other debugger then we can add the breakpoint where the run method is called and step into it. We can see the following execution in the run method.
Here, inside the run() method (\Magento\Framework\App\Bootstrap):
Magento checks for the errors first using $this->initErrorHandler(). If any error found it will throw the Exception and stop the further execution.
After that it checks for maintenance mode using $this->assertMaintenance().If maintenance mode is enabled then it will stop the further execution of the application with Exception.
After checking the maintenance mode, it will check that if the Magento is installed or not using $this->assertInstalled().If Magento is not installed then Magento installation setup will be started.
After checking the $this->assertInstalled() method. The $application->launch() method is called as you can see in the screenshot.
Step 3 – launch()
You can find the launch() method inside \Magento\Framework\App\Http class.
Here in the launch() method, Magento set the area code and create the instance of
$frontController = $this->_objectManager->get(\Magento\Framework\App\FrontControllerInterface::class);
And $frontController calls the dispatch() method.
The dispatch() will be executed from \Magento\Framework\App\FrontController class. But before executing the actual dispatch() method, Magento has an aroundDispatch() interceptor/plugin in \Magento\PageCache\Model\App\FrontController\BuiltinPlugin class, which is executed only if the full page cache is enabled and returns the response accordingly without executing the actual dispatch() method.
Step 4 – dispatch()
This method loads the available router list and match the request with available routers. Which then finds the correct action class.
Dispatch() method is implemented in Magento\Framework\App\Action\Action.php.
When we create any custom actions, then these actions are inherited from this class.
Action controller returns the object that realizes ResultInterface via execute() method.
Finally, FrontController returns ResultInterface into Application Instance, which puts out a
response.