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

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.

主站蜘蛛池模板: 肥西县| 泰州市| 元氏县| 买车| 山东省| 辽阳县| 台山市| 白银市| 万年县| 乌恰县| 紫阳县| 仁布县| 阿克陶县| 冀州市| 蓝田县| 铜梁县| 辽源市| 友谊县| 托克逊县| 昆明市| 旺苍县| 利辛县| 张家口市| 德州市| 黔东| 玉溪市| 内江市| 泰顺县| 教育| 莫力| 台江县| 克东县| 东城区| 甘泉县| 巴彦县| 株洲县| 弋阳县| 壶关县| 始兴县| 吴江市| 吕梁市|