@@ -69,13 +69,14 @@ In HTTP-speak, this HTTP request would actually look something like this:
6969
7070 This simple message communicates *everything * necessary about exactly which
7171resource the client is requesting. The first line of an HTTP request is the
72- most important and contains two things: the URI and the HTTP method.
72+ most important, because it contains two important things: the HTTP method (GET)
73+ and the URL (``/ ``).
7374
7475The URI (e.g. ``/ ``, ``/contact ``, etc) is the unique address or location
7576that identifies the resource the client wants. The HTTP method (e.g. ``GET ``)
76- defines what you want to *do * with the resource. The HTTP methods are the
77- * verbs * of the request and define the few common ways that you can act upon
78- the resource :
77+ defines what the client wants to *do * with the resource. The HTTP methods (also
78+ known as verbs) define the few common ways that the client can act upon the
79+ resource - the most common HTTP methods are :
7980
8081+----------+---------------------------------------+
8182| *GET * | Retrieve the resource from the server |
@@ -96,18 +97,18 @@ delete a specific blog entry, for example:
9697
9798 .. note ::
9899
99- There are actually nine HTTP methods (also known as verbs) defined by
100- the HTTP specification, but many of them are not widely used or supported.
101- In reality, many modern browsers only support ``POST `` and ``GET `` in
102- HTML forms. Various others are however supported in XMLHttpRequests,
103- as well as by Symfony's router.
100+ There are actually nine HTTP methods defined by the HTTP specification,
101+ but many of them are not widely used or supported. In reality, many
102+ modern browsers only support ``POST `` and ``GET `` in HTML forms. Various
103+ others are however supported in ` XMLHttpRequest `_, as well as by Symfony's
104+ :doc: ` Routing component < /components/routing/introduction >`.
104105
105106In addition to the first line, an HTTP request invariably contains other
106- lines of information called request headers. The headers can supply a wide
107- range of information such as the requested ``Host ``, the response formats
108- the client accepts (``Accept ``) and the application the client is using to
109- make the request (``User-Agent ``). Many other headers exist and can be found
110- on Wikipedia's `List of HTTP header fields `_ article.
107+ lines of information called request ** headers ** . The headers can supply a wide
108+ range of information such as the host of the resource being requested ( ``Host ``),
109+ the response formats the client accepts (``Accept ``) and the application the
110+ client is using to make the request (``User-Agent ``). Many other headers exist
111+ and can be found on Wikipedia's `List of HTTP header fields `_ article.
111112
112113Step 2: The Server Returns a Response
113114~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -138,18 +139,17 @@ like this:
138139 The HTTP response contains the requested resource (the HTML content in this
139140case), as well as other information about the response. The first line is
140141especially important and contains the HTTP response status code (200 in this
141- case). The status code communicates the overall outcome of the request back
142- to the client. Was the request successful? Was there an error? Different
143- status codes exist that indicate success, an error, or that the client needs
144- to do something (e.g. redirect to another page). A full list can be found
145- on Wikipedia's ` List of HTTP status codes `_ article.
142+ case). The status code communicates the overall outcome of the request back to the
143+ client. Was the request successful? Was there an error? Different status codes exist
144+ that indicate success, an error, or that the client needs to do something (e.g.
145+ redirect to another page). A full list can be found on Wikipedia's ` List of HTTP status codes `_
146+ article.
146147
147148Like the request, an HTTP response contains additional pieces of information
148- known as HTTP headers. For example, one important HTTP response header is
149- ``Content-Type ``. The body of the same resource could be returned in multiple
149+ known as HTTP headers. The body of the same resource could be returned in multiple
150150different formats like HTML, XML, or JSON and the ``Content-Type `` header uses
151151Internet Media Types like ``text/html `` to tell the client which format is
152- being returned. You can see a `list of common media types `_ from IANA.
152+ being returned. You can see a `List of common media types `_ from IANA.
153153
154154Many other headers exist, some of which are very powerful. For example, certain
155155headers can be used to create a powerful caching system.
@@ -196,7 +196,7 @@ from the HTTP request and using it to create an HTTP response. Instead of
196196parsing the raw HTTP request message, PHP prepares superglobal variables
197197such as ``$_SERVER `` and ``$_GET `` that contain all the information from
198198the request. Similarly, instead of returning the HTTP-formatted text response,
199- you can use the ``header() `` function to create response headers and simply
199+ you can use the PHP ``header() `` function to create response headers and simply
200200print out the actual content that will be the content portion of the response
201201message. PHP will create a true HTTP response and return it to the client:
202202
@@ -215,6 +215,10 @@ Requests and Responses in Symfony
215215
216216Symfony provides an alternative to the raw PHP approach via two classes that
217217allow you to interact with the HTTP request and response in an easier way.
218+
219+ Symfony Request Object
220+ ~~~~~~~~~~~~~~~~~~~~~~
221+
218222The :class: `Symfony\\ Component\\ HttpFoundation\\ Request ` class is a simple
219223object-oriented representation of the HTTP request message. With it, you
220224have all the request information at your fingertips::
@@ -226,17 +230,17 @@ have all the request information at your fingertips::
226230 // the URI being requested (e.g. /about) minus any query parameters
227231 $request->getPathInfo();
228232
229- // retrieve GET and POST variables respectively
233+ // retrieve $_GET and $_POST variables respectively
230234 $request->query->get('foo');
231235 $request->request->get('bar', 'default value if bar does not exist');
232236
233- // retrieve SERVER variables
237+ // retrieve $_SERVER variables
234238 $request->server->get('HTTP_HOST');
235239
236240 // retrieves an instance of UploadedFile identified by foo
237241 $request->files->get('foo');
238242
239- // retrieve a COOKIE value
243+ // retrieve a $_COOKIE value
240244 $request->cookies->get('PHPSESSID');
241245
242246 // retrieve an HTTP request header, with normalized, lowercase keys
@@ -273,34 +277,49 @@ the user is connecting via a secured connection (i.e. HTTPS).
273277 ``attributes `` property exists entirely to be a place where you can
274278 prepare and store context-specific information about the request.
275279
276- Symfony also provides a ``Response `` class: a simple PHP representation of
277- an HTTP response message. This allows your application to use an object-oriented
278- interface to construct the response that needs to be returned to the client::
280+ Symfony Response Object
281+ ~~~~~~~~~~~~~~~~~~~~~~~
282+
283+ Symfony also provides a :class: `Symfony\\ Component\\ HttpFoundation\\ Response `
284+ class: a simple PHP representation of an HTTP response message. This allows your
285+ application to use an object-oriented interface to construct the response that
286+ needs to be returned to the client::
279287
280288 use Symfony\Component\HttpFoundation\Response;
281289
282290 $response = new Response();
283291
284292 $response->setContent('<html><body><h1>Hello world!</h1></body></html>');
285293 $response->setStatusCode(Response::HTTP_OK);
294+
295+ // set a HTTP response header
286296 $response->headers->set('Content-Type', 'text/html');
287297
288- // prints the HTTP headers followed by the content
298+ // print the HTTP headers followed by the content
289299 $response->send();
290300
301+ There are also special classes to make certain types of responses easier to create:
302+
303+ * :ref: `JsonResponse <component-http-foundation-json-response >`;
304+
305+ * :ref: `BinaryFileResponse <component-http-foundation-serving-files >` (for streaming
306+ files and sending file downloads);
307+
308+ * :ref: `StreamedResponse <streaming-response >` (for streaming any other large responses);
309+
310+ .. tip ::
311+
312+ The ``Request `` and ``Response `` classes are part of a standalone component
313+ called :doc: `symfony/http-foundation </components/http_foundation/introduction >`
314+ that yo can use in *any * PHP project. This also contains classes for handling
315+ sessions, file uploads and more.
316+
291317If Symfony offered nothing else, you would already have a toolkit for easily
292318accessing request information and an object-oriented interface for creating
293319the response. Even as you learn the many powerful features in Symfony, keep
294320in mind that the goal of your application is always *to interpret a request
295321and create the appropriate response based on your application logic *.
296322
297- .. tip ::
298-
299- The ``Request `` and ``Response `` classes are part of a standalone component
300- included with Symfony called HttpFoundation. This component can be
301- used entirely independently of Symfony and also provides classes for handling
302- sessions and file uploads.
303-
304323The Journey from the Request to the Response
305324--------------------------------------------
306325
@@ -316,6 +335,9 @@ code organized and maintainable?
316335
317336Symfony was created to solve these problems so that you don't have to.
318337
338+ .. index ::
339+ single: Front controller; Origins
340+
319341The Front Controller
320342~~~~~~~~~~~~~~~~~~~~
321343
@@ -347,9 +369,8 @@ handles every request coming into your application. For example:
347369
348370.. tip ::
349371
350- Using Apache's ``mod_rewrite `` (or equivalent with other web servers),
351- the URLs can easily be cleaned up to be just ``/ ``, ``/contact `` and
352- ``/blog ``.
372+ By using rewrite rules in your :doc: `web server configuration </cookbook/configuration/web_server_configuration >`,
373+ the ``index.php `` won't be needed and you will have beautiful, clean URLs (e.g. ``/show ``).
353374
354375Now, every request is handled exactly the same way. Instead of individual URLs
355376executing different PHP files, the front controller is *always * executed,
@@ -384,6 +405,9 @@ on that value. This can get ugly quickly::
384405Solving this problem can be difficult. Fortunately it's *exactly * what Symfony
385406is designed to do.
386407
408+ .. index ::
409+ single: HTTP; Symfony request flow
410+
387411The Symfony Application Flow
388412~~~~~~~~~~~~~~~~~~~~~~~~~~~~
389413
@@ -396,8 +420,8 @@ the same simple pattern for every request:
396420 :align: center
397421 :alt: Symfony request flow
398422
399- Incoming requests are interpreted by the routing and passed to controller
400- functions that return ``Response `` objects.
423+ Incoming requests are interpreted by the :doc: `Routing component </book/routing>` and
424+ passed to PHP functions that return ``Response `` objects.
401425
402426Each "page" of your site is defined in a routing configuration file that
403427maps different URLs to different PHP functions. The job of each PHP function,
@@ -408,13 +432,17 @@ you interpret the request and create a response.
408432
409433It's that easy! To review:
410434
411- * Each request executes a front controller file;
435+ * Each request executes the same, single file (called a "front controller");
436+
437+ * The front controller boots Symfony, and passes it request information;
412438
413- * The routing system determines which PHP function should be executed based
414- on information from the request and routing configuration you've created ;
439+ * The router matches the incoming URL to a specific route and returns information
440+ about the route, including the controller (i.e. function) that should be executed ;
415441
416- * The correct PHP function is executed, where your code creates and returns
417- the appropriate ``Response `` object.
442+ * The controller (function) is executed: this is where *your * code creates and
443+ returns the appropriate ``Response `` object;
444+
445+ * The HTTP headers and content of the ``Response `` object are sent back to the client.
418446
419447A Symfony Request in Action
420448~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -460,9 +488,10 @@ by adding an entry for ``/contact`` to your routing configuration file:
460488 return $collection;
461489
462490 When someone visits the ``/contact `` page, this route is matched, and the
463- specified controller is executed. As you'll learn in the :doc: `routing chapter </book/routing >`,
464- the ``AppBundle:Main:contact `` string is a short syntax that points to a
465- specific PHP method ``contactAction `` inside a class called ``MainController ``::
491+ specified controller is executed. As you'll learn in the
492+ :ref: `routing chapter <controller-string-syntax >`, the ``AppBundle:Main:contact ``
493+ string is a short syntax that points to a specific controller - ``contactAction() `` -
494+ inside a controller class called - ``MainController ``::
466495
467496 // src/AppBundle/Controller/MainController.php
468497 namespace AppBundle\Controller;
@@ -477,9 +506,9 @@ specific PHP method ``contactAction`` inside a class called ``MainController``::
477506 }
478507 }
479508
480- In this very simple example, the controller simply creates a
509+ In this example, the controller creates a
481510:class: `Symfony\\ Component\\ HttpFoundation\\ Response ` object with the HTML
482- ``<h1>Contact us!</h1> ``. In the :doc: `controller chapter </book/controller >`,
511+ ``<h1>Contact us!</h1> ``. In the :doc: `Controller chapter </book/controller >`,
483512you'll learn how a controller can render templates, allowing your "presentation"
484513code (i.e. anything that actually writes out HTML) to live in a separate
485514template file. This frees up the controller to worry only about the hard
@@ -504,7 +533,7 @@ tools. With Symfony, nothing is imposed on you: you're free to use the full
504533Symfony Framework, or just one piece of Symfony all by itself.
505534
506535.. index ::
507- single: Symfony Components
536+ single: Symfony Components
508537
509538.. _standalone-tools-the-symfony2-components :
510539
@@ -522,8 +551,8 @@ regardless of how your project is developed. To name a few:
522551
523552:doc: `Routing </components/routing/introduction >`
524553 Powerful and fast routing system that allows you to map a specific URI
525- (e.g. ``/contact ``) to some information about how that request should be handled
526- (e.g. execute the ``contactAction() `` method).
554+ (e.g. ``/contact ``) to information about how that request should be handled (e.g.
555+ that the ``contactAction() `` controller method should be executed ).
527556
528557:doc: `Form </components/form/introduction >`
529558 A full-featured and flexible framework for creating forms and handling form
@@ -572,11 +601,12 @@ by using a Symfony distribution, which provides a project skeleton with
572601sensible defaults. For more advanced users, the sky is the limit.
573602
574603.. _`xkcd` : http://xkcd.com/
604+ .. _`XMLHttpRequest` : https://en.wikipedia.org/wiki/XMLHttpRequest
575605.. _`HTTP 1.1 RFC` : http://www.w3.org/Protocols/rfc2616/rfc2616.html
576606.. _`HTTP Bis` : http://datatracker.ietf.org/wg/httpbis/
577607.. _`Live HTTP Headers` : https://addons.mozilla.org/en-US/firefox/addon/live-http-headers/
578- .. _`List of HTTP status codes` : https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
579608.. _`List of HTTP header fields` : https://en.wikipedia.org/wiki/List_of_HTTP_header_fields
580- .. _`list of common media types` : https://www.iana.org/assignments/media-types/media-types.xhtml
609+ .. _`List of HTTP status codes` : https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
610+ .. _`List of common media types` : https://www.iana.org/assignments/media-types/media-types.xhtml
581611.. _`Validator` : https://github.com/symfony/validator
582612.. _`Swift Mailer` : http://swiftmailer.org/
0 commit comments