@@ -69,13 +69,14 @@ In HTTP-speak, this HTTP request would actually look something like this:
69
69
70
70
This simple message communicates *everything * necessary about exactly which
71
71
resource 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 (``/ ``).
73
74
74
75
The URI (e.g. ``/ ``, ``/contact ``, etc) is the unique address or location
75
76
that 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 :
79
80
80
81
+----------+---------------------------------------+
81
82
| *GET * | Retrieve the resource from the server |
@@ -96,18 +97,18 @@ delete a specific blog entry, for example:
96
97
97
98
.. note ::
98
99
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 >`.
104
105
105
106
In 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.
111
112
112
113
Step 2: The Server Returns a Response
113
114
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -138,18 +139,17 @@ like this:
138
139
The HTTP response contains the requested resource (the HTML content in this
139
140
case), as well as other information about the response. The first line is
140
141
especially 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.
146
147
147
148
Like 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
150
150
different formats like HTML, XML, or JSON and the ``Content-Type `` header uses
151
151
Internet 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.
153
153
154
154
Many other headers exist, some of which are very powerful. For example, certain
155
155
headers 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
196
196
parsing the raw HTTP request message, PHP prepares superglobal variables
197
197
such as ``$_SERVER `` and ``$_GET `` that contain all the information from
198
198
the 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
200
200
print out the actual content that will be the content portion of the response
201
201
message. PHP will create a true HTTP response and return it to the client:
202
202
@@ -215,6 +215,10 @@ Requests and Responses in Symfony
215
215
216
216
Symfony provides an alternative to the raw PHP approach via two classes that
217
217
allow you to interact with the HTTP request and response in an easier way.
218
+
219
+ Symfony Request Object
220
+ ~~~~~~~~~~~~~~~~~~~~~~
221
+
218
222
The :class: `Symfony\\ Component\\ HttpFoundation\\ Request ` class is a simple
219
223
object-oriented representation of the HTTP request message. With it, you
220
224
have all the request information at your fingertips::
@@ -226,17 +230,17 @@ have all the request information at your fingertips::
226
230
// the URI being requested (e.g. /about) minus any query parameters
227
231
$request->getPathInfo();
228
232
229
- // retrieve GET and POST variables respectively
233
+ // retrieve $_GET and $_POST variables respectively
230
234
$request->query->get('foo');
231
235
$request->request->get('bar', 'default value if bar does not exist');
232
236
233
- // retrieve SERVER variables
237
+ // retrieve $_SERVER variables
234
238
$request->server->get('HTTP_HOST');
235
239
236
240
// retrieves an instance of UploadedFile identified by foo
237
241
$request->files->get('foo');
238
242
239
- // retrieve a COOKIE value
243
+ // retrieve a $_COOKIE value
240
244
$request->cookies->get('PHPSESSID');
241
245
242
246
// retrieve an HTTP request header, with normalized, lowercase keys
@@ -273,34 +277,49 @@ the user is connecting via a secured connection (i.e. HTTPS).
273
277
``attributes `` property exists entirely to be a place where you can
274
278
prepare and store context-specific information about the request.
275
279
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::
279
287
280
288
use Symfony\Component\HttpFoundation\Response;
281
289
282
290
$response = new Response();
283
291
284
292
$response->setContent('<html><body><h1>Hello world!</h1></body></html>');
285
293
$response->setStatusCode(Response::HTTP_OK);
294
+
295
+ // set a HTTP response header
286
296
$response->headers->set('Content-Type', 'text/html');
287
297
288
- // prints the HTTP headers followed by the content
298
+ // print the HTTP headers followed by the content
289
299
$response->send();
290
300
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
+
291
317
If Symfony offered nothing else, you would already have a toolkit for easily
292
318
accessing request information and an object-oriented interface for creating
293
319
the response. Even as you learn the many powerful features in Symfony, keep
294
320
in mind that the goal of your application is always *to interpret a request
295
321
and create the appropriate response based on your application logic *.
296
322
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
-
304
323
The Journey from the Request to the Response
305
324
--------------------------------------------
306
325
@@ -316,6 +335,9 @@ code organized and maintainable?
316
335
317
336
Symfony was created to solve these problems so that you don't have to.
318
337
338
+ .. index ::
339
+ single: Front controller; Origins
340
+
319
341
The Front Controller
320
342
~~~~~~~~~~~~~~~~~~~~
321
343
@@ -347,9 +369,8 @@ handles every request coming into your application. For example:
347
369
348
370
.. tip ::
349
371
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 ``).
353
374
354
375
Now, every request is handled exactly the same way. Instead of individual URLs
355
376
executing different PHP files, the front controller is *always * executed,
@@ -384,6 +405,9 @@ on that value. This can get ugly quickly::
384
405
Solving this problem can be difficult. Fortunately it's *exactly * what Symfony
385
406
is designed to do.
386
407
408
+ .. index ::
409
+ single: HTTP; Symfony request flow
410
+
387
411
The Symfony Application Flow
388
412
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
389
413
@@ -396,8 +420,8 @@ the same simple pattern for every request:
396
420
:align: center
397
421
:alt: Symfony request flow
398
422
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.
401
425
402
426
Each "page" of your site is defined in a routing configuration file that
403
427
maps different URLs to different PHP functions. The job of each PHP function,
@@ -408,13 +432,17 @@ you interpret the request and create a response.
408
432
409
433
It's that easy! To review:
410
434
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;
412
438
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 ;
415
441
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.
418
446
419
447
A Symfony Request in Action
420
448
~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -460,9 +488,10 @@ by adding an entry for ``/contact`` to your routing configuration file:
460
488
return $collection;
461
489
462
490
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 ``::
466
495
467
496
// src/AppBundle/Controller/MainController.php
468
497
namespace AppBundle\Controller;
@@ -477,9 +506,9 @@ specific PHP method ``contactAction`` inside a class called ``MainController``::
477
506
}
478
507
}
479
508
480
- In this very simple example, the controller simply creates a
509
+ In this example, the controller creates a
481
510
: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 >`,
483
512
you'll learn how a controller can render templates, allowing your "presentation"
484
513
code (i.e. anything that actually writes out HTML) to live in a separate
485
514
template 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
504
533
Symfony Framework, or just one piece of Symfony all by itself.
505
534
506
535
.. index ::
507
- single: Symfony Components
536
+ single: Symfony Components
508
537
509
538
.. _standalone-tools-the-symfony2-components :
510
539
@@ -522,8 +551,8 @@ regardless of how your project is developed. To name a few:
522
551
523
552
:doc: `Routing </components/routing/introduction >`
524
553
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 ).
527
556
528
557
:doc: `Form </components/form/introduction >`
529
558
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
572
601
sensible defaults. For more advanced users, the sky is the limit.
573
602
574
603
.. _`xkcd` : http://xkcd.com/
604
+ .. _`XMLHttpRequest` : https://en.wikipedia.org/wiki/XMLHttpRequest
575
605
.. _`HTTP 1.1 RFC` : http://www.w3.org/Protocols/rfc2616/rfc2616.html
576
606
.. _`HTTP Bis` : http://datatracker.ietf.org/wg/httpbis/
577
607
.. _`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
579
608
.. _`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
581
611
.. _`Validator` : https://github.com/symfony/validator
582
612
.. _`Swift Mailer` : http://swiftmailer.org/
0 commit comments