Drupal 8/9: Request to Response Simplified

Mohit Wadhwa
2 min readJun 9, 2021
Photo by Stackie Jia on Unsplash

In this article, I will be analysing, with a simple example, on what happens internally in Drupal as soon as a user makes a request till the time user is served a response.
The usual scenario is that a request is received from the browser and response is served from the server.
Following are the steps in a flow from request to response:

  1. A user accesses the http://example.com/node/1 URL in browser.
  2. The browser contacts the server at example.com and requests the resource at /node/1.
  3. The web server then recognizes that the request must be handled PHP and contact PHP environment to handle the request.
  4. PHP, then, executes the Drupal’s front controller file (index.php), which in turn creates a new Request Object from the requested resource.
  5. The request object is handled by Symfony’s HTTPKernel by dispatching the events like kernel.request, kernel.controller, kernel.response and kernel.view.
  6. The responsible route is identified through the kernel.request event and route controller is identified.
  7. As the route, in our example, is registered by the Node module which identifies the entity ID, loads the entity and finaly builds the markup to be returned as a part of response.
  8. If the respective controller returns something other than a response object, then kernel.view event comes in picture and is dispatched to check if there is something that can transform it into a response object. Like controller returns a rendered array which is finally transformed to response object.
  9. As soon the response is created, the front controller returns the response to the browser and the request is terminated.

We don’t really depend on Drupal to transform our render array into response, we can do it ourselves directly while doing custom code.
I tried to cover the request to response is a simplified way, if it helped you in any way then do hit the clap/like button.

--

--