From request to response

Now that we have listed the most important architectural pieces of Drupal, let's briefly see how these are used in delivering responses to the requests a user makes on a Drupal 8 website. To this end, we will analyze a simplified example of a request as it is handled on a Drupal 8 website:

  1. A user accesses the http://example.com/node/123 URL in a web browser.
  2. The browser contacts the web server at example.com and requests the resource at /node/123.
  3. The web server recognizes that the request must be handled by PHP and starts up (or contacts) a PHP environment to handle the request.
  4. PHP executes Drupal's front controller file (index.php), which then creates a new Request object from the resource that was requested.
  5. Symfony's HTTPKernel handles this request object by dispatching a number of events, such as kernel.request, kernel.controller, kernel.response, and kernel.view.
  6. The route that maps to that request is identified through the kernel.request event.
  7. The route controller is identified, and the kernel.controller event is used to perform any alterations on the responsible controller, as well as to resolve the arguments that need to be passed to it. In our case, this route is registered by the Node module through the main Entity system, which identifies the entity ID, loads it, and builds the markup to be returned as part of the response.
  1. If the respective controller (or handler) returns something other than a response object, the kernel.view event is dispatched to check whether there is any code that can transform that into a Response object. In most cases, in Drupal 8, we typically return render arrays, which are transformed into Response objects.
  2. Once a Response is created, the front controller returns it to the browser and terminates the request.

In this context, as Drupal 8 module developers, we spend most of our time inside controllers and services, trying to figure out what we need to return to the page. We then rely on Drupal to transform our render array into a proper response to the user, but we can also return one ourselves directly. Moreover, the theme system comes into play here, as well as the block system, because our content gets wrapped into a block that is placed in a region surrounded by other regions that contain other blocks. If it sounds complicated now, don't worry; we will cover in detail all these aspects with examples, and it will become clear in no time.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset