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

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.

主站蜘蛛池模板: 桃园市| 万盛区| 安塞县| 崇礼县| 连南| 老河口市| 井陉县| 天峻县| 新蔡县| 丹棱县| 道真| 家居| 乳源| 喀什市| 邯郸县| 休宁县| 平原县| 蓬溪县| 神木县| 临沭县| 东明县| 留坝县| 闽清县| 西吉县| 陈巴尔虎旗| 安远县| 九龙城区| 潜山县| 余江县| 大悟县| 玉龙| 隆尧县| 博罗县| 漯河市| 青龙| 沧源| 孟村| 宁明县| 宣化县| 南靖县| 拜泉县|