官术网_书友最值得收藏!

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 very simplified example of a typical request as it is handled on a Drupal 8 website:

  • A user enters the http://example.com/node/123 URL in a web browser and presses Enter.
  • The browser contacts the web server at example.com and requests the resource at /node/123.
  • The web server recognizes that the request must be handled by PHP and starts up (or contacts) a PHP environment to handle the request.
  • PHP executes Drupal's front controller file (index.php), which then creates a new Request object from the resource that was requested.
  • Symfony's HTTPKernel handles this request object by dispatching a number of events, such as kernel.request, kernel.controller, kernel.response, and kernel.view.
  • The route that maps to that request is identified through the kernel.request event.
  • The route controller is identified, and the kernel.controller event is used to perform any alterations on the used controller and 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 Response.
  • 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.
  • 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 times 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 to be wrapped into a block that is placed in a region surrounded by other regions that contain blocks. However, 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.

However, as a quick conclusion, we can see that events are mostly used (but not only) at the highest levels of a request, whereas plugins and hooks are mostly used at lower levels, within the process of calculating, building a page, or handling a specific business logic.

主站蜘蛛池模板: 康定县| 丘北县| 黄骅市| 谢通门县| 容城县| 桂东县| 乌鲁木齐县| 金坛市| 南乐县| 宝丰县| 会泽县| 科尔| 遵义市| 澄城县| 泰来县| 普格县| 阳曲县| 运城市| 枣强县| 印江| 喜德县| 盐边县| 东安县| 抚远县| 曲靖市| 汶川县| 美姑县| 昌图县| 深水埗区| 陇川县| 英山县| 永定县| 扎鲁特旗| 山阴县| 库车县| 桂平市| 安徽省| 明水县| 太保市| 社旗县| 墨玉县|