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

  • Mastering Symfony
  • Sohail Salehi
  • 468字
  • 2021-07-16 11:28:58

The big picture

The request/response life cycle can be summarized in these two simple steps:

  1. Firstly, you send your request by entering a URL in your browser.
  2. The server then responds with a page and message (success, failure, and so on) depending on your request. End of story.

The following image shows an example of the request/response life cycle:

The big picture

A web server receives a request and passes it to an action unit for further processing. In our case, this action unit is somewhere in Symfony and is in charge of receiving requests. Depending on their type, it will fetch a resource (such as a record from the database or an image from the server's hard drive) or do something (like sending an e-mail or assembling and returning a JavaScript Object Notation (JSON) string). Finally, it renders a page based on the results and sends it back to the browser. After the job is done, this action unit marks the request and response as terminated and looks for the next request, as shown in the following diagram:

The big picture

The general request/response life cycle on a server with Symfony

Look at your /web folder in your Symfony installation from the previous chapter. You can see that there is an app_dev.php file over there. Open the file (or the app.php file if you like) and pay attention to the last four lines:

$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);

These lines summarize the preceding story beautifully.

You can see how it is represented in the following screenshot. These are the four steps that Symfony takes to process a request and send a response:

The big picture

Well, that's the purpose of using Symfony. It sits on the web server and makes it serve for each receiving request. The second line calls a handle() method. This method is the main reason why we are here. It might look like just one innocent method, but in fact, the handle() method is in charge of managing other units that deal with databases, JSON strings, REST and SOAP requests, processing e-mails, rendering templates, and who knows what else in the future. Note that the handle() method manages the incoming requests by finding (routing) the right controller action and getting a response from it. However, it doesn't personally do the job itself. So do not underestimate the method. It might not look like doing much, but it controls everything. It makes the server components dance on the arrival of every single request.

Let's put this information in our big picture and see how it looks:

The big picture

The handle() method facilitates the flow between the browser and server

Now we have a good template to refer to. In each of the following topics, I will update this big picture so that you can get the idea of each concept easily.

主站蜘蛛池模板: 琼海市| 山东省| 彭泽县| 罗江县| 瑞金市| 德昌县| 含山县| 黑龙江省| 阿合奇县| 贞丰县| 沁源县| 西华县| 芷江| 临泽县| 临海市| 濮阳市| 葫芦岛市| 遂平县| 邯郸县| 丽水市| 明水县| 盐城市| 武义县| 闻喜县| 松桃| 桓台县| 富裕县| 文化| 敦煌市| 阳信县| 绥滨县| 万山特区| 鹤山市| 龙南县| 黄大仙区| 临猗县| 凯里市| 文水县| 东源县| 静乐县| 新营市|