Mastering Symfony
上QQ阅读APP看书,第一时间看更新

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.