A deep dive into magento’s sophisticated request flow process.

How Requests Navigate to Controller Execute Method in Magento

Mobeen Sarwar
mobeensarwar
Published in
3 min readJun 16, 2024

--

Photo by Jeremy Bishop on Unsplash

Magento is a powerful and complex eCommerce platform, and understanding how it processes a request to the point where a controller’s executemethod is called, can be quite enlightening. In this article, we’ll walk through the steps of Magento’s request flow, from the initial request to the final execution by the controller.

1. Initializing the Application

Magento\Framework\App\Bootstrap->run() in pub/index.php

Every Magento request begins its journey at pub/index.php, the entry point for all web requests. Here, the Magento\Framework\App\Bootstrap class's run() method is called. This method is responsible for initializing the application and handling the request.

2. Launching the Application

Magento\Framework\App\Http->launch() in vendor/magento/framework/App/Bootstrap.php

Next, the Bootstrap->run() method calls the launch() method of the Magento\Framework\App\Http class. This method is responsible for launching the application and handling the HTTP request.

3. Routing the Request

Magento\Framework\App\FrontController\Interceptor->dispatch() in vendor/magento/framework/App/Http.php

The launch() method calls the dispatch() method of the FrontController class. The FrontController is responsible for routing the request to the appropriate controller.

4. Processing the Request

Magento\Framework\App\FrontController->processRequest() in vendor/magento/framework/App/FrontController.php

Within the dispatch() method, the processRequest() method of FrontController is called. This method processes the request and determines which controller should handle it.

Magento processes a request through a series of method calls within the FrontController class. Here’s a concise breakdown of the flow:

Execution Flow:

1. dispatch(RequestInterface $request)

  • Initiates profiling and sets up a loop to match the request with a router.
  • Attempts to find a matching action instance for the request.
  • If no match is found, forwards the request to a “no route” action.

2. processRequest(RequestInterface $request, ActionInterface $actionInstance)

  • Marks the request as dispatched and sets no-cache headers.
  • Validates the request, loading necessary areas and handling any validation exceptions.
  • Dispatches pre and post-dispatch events and retrieves the action response.

3. getActionResponse(ActionInterface $actionInstance, RequestInterface $request)

  • Checks for a no-dispatch flag and returns the response if set.
  • Uses the deprecated dispatch() method if the action instance is an instance of AbstractAction.
  • Calls the execute() method of the action instance, which is the correct and modern way to handle the request.

5. Dispatching the Action

Magento\Framework\App\Action\Action->dispatch() in vendor/magento/module-backend/App/AbstractAction.php

The dispatch() method of the Action class is called next. This method dispatches the request to the appropriate action method in the controller.

6. Executing the Controller Method

TestModule\TestRequest\Controller\Adminhtml\Index\Index->execute() in vendor/magento/framework/App/Action/Action.php

Finally, the execute() method of the specific controller is called. TestModule\TestRequest\Controller\Adminhtml\Index\Index->execute()
This method contains the business logic that handles the request and generates the response.

<?php
namespace TestModule\TestRequest\Controller\Adminhtml\Index;

use Magento\Backend\App\Action;

class Index extends Action
{
public function execute() {
// Your controller logic here
}

}

By understanding these steps, you gain insight into how Magento handles requests from the moment they enter the system until the controller’s execute method is called. This knowledge is crucial for debugging and extending Magento’s functionality. Stay tuned for Part 2, where we’ll dive deeper into the specifics of controller actions and responses.

--

--