6
6
Symfony and HTTP Fundamentals
7
7
=============================
8
8
9
- Symfony is modeled after the HTTP Response-Request flow. This means that
10
- knowing HTTP fundamentals is an important part of understanding Symfony.
11
- Fortunately, understanding a basic Request-Response flow in HTTP is not
12
- difficult. This chapter will walk you through the HTTP fundamental basics and
13
- what this means for Symfony.
9
+ Great news! While you're learning Symfony, you're *also * learning the fundamentals
10
+ of the *web *. Symfony is closely modeled after the HTTP Request-Response flow: that
11
+ *fundamental * paradigm that's behind almost *all * communication on the web.
12
+
13
+ In this chapter, you'll walk through the HTTP fundamentals and find out how these
14
+ are applied throughout Symfony.
14
15
15
16
Requests and Responses in HTTP
16
17
------------------------------
@@ -261,6 +262,12 @@ needs to be returned to the client::
261
262
// print the HTTP headers followed by the content
262
263
$response->send();
263
264
265
+ There are also several response *sub-classes * to help you return
266
+ :ref: `JSON <component-http-foundation-json-response >`,
267
+ :ref: `redirect <redirect-response >`,
268
+ :ref: `stream file downloads <component-http-foundation-serving-files >`
269
+ and more.
270
+
264
271
.. tip ::
265
272
266
273
The ``Request `` and ``Response `` classes are part of a standalone component
@@ -314,27 +321,43 @@ handles every request coming into your application. For example:
314
321
| ``/index.php/blog `` | executes ``index.php `` |
315
322
+------------------------+------------------------+
316
323
317
- .. tip ::
318
-
319
- By using rewrite rules in your
320
- :doc: `web server configuration </cookbook/configuration/web_server_configuration >`,
321
- the ``index.php `` won't be needed and you will have beautiful, clean URLs
322
- (e.g. ``/show ``).
324
+ .. include :: includes/_rewrite_rule_tip.rst.inc
323
325
324
326
Now, every request is handled exactly the same way. Instead of individual URLs
325
327
executing different PHP files, the front controller is *always * executed,
326
328
and the routing of different URLs to different parts of your application
327
- is done internally. This solves both problems with the original approach.
328
- Almost all modern web apps do this.
329
+ is done internally.
330
+
331
+ A very simple front controller might look like this::
332
+
333
+ // index.php
334
+ use Symfony\Component\HttpFoundation\Request;
335
+ use Symfony\Component\HttpFoundation\Response;
336
+
337
+ $request = Request::createFromGlobals();
338
+ $path = $request->getPathInfo(); // the URI path being requested
339
+
340
+ if (in_array($path, array('', '/'))) {
341
+ $response = new Response('Welcome to the homepage.');
342
+ } elseif ('/contact' === $path) {
343
+ $response = new Response('Contact us');
344
+ } else {
345
+ $response = new Response('Page not found.', Response::HTTP_NOT_FOUND);
346
+ }
347
+ $response->send();
348
+
349
+ This is better, but this is still a lot of repeated work! Fortunately, Symfony can
350
+ help once again.
329
351
330
352
.. index ::
331
353
single: HTTP; Symfony request flow
332
354
333
355
The Symfony Application Flow
334
356
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
335
357
336
- Symfony will operate in this front-controller file to handle each incoming
337
- request. Symfony follows the same simple pattern for every request:
358
+ A Symfony framework application *also * uses a front-controller file. But inside,
359
+ *Symfony * is responsible for handling each incoming request and figuring out what
360
+ to do:
338
361
339
362
.. _request-flow-figure :
340
363
@@ -345,34 +368,25 @@ request. Symfony follows the same simple pattern for every request:
345
368
Incoming requests are interpreted by the :doc: `Routing component </book/routing>` and
346
369
passed to PHP functions that return ``Response `` objects.
347
370
348
- Each "page" of your site is defined in a routing configuration file that
349
- maps different URLs to different PHP functions. The job of each PHP function,
350
- called a controller, is to use information from the request - along with many
351
- other tools Symfony makes available - to create and return a ``Response ``
352
- object. In other words, the controller is where *your * code goes: it's where
353
- you interpret the request and create a response.
371
+ This may not make sense yet, but as you keep reading, you'll learn about :ref: `routes </routing >`
372
+ and :ref: `controllers </controller >`: the two fundamental parts to creating a page.
373
+ But as you go along, don't forget that no matter *how * complex your app gets, your
374
+ job is always the same: read information from the Request and use it to create a
375
+ Response.
354
376
355
- Conclusion
356
- ----------
377
+ Summary: The Request-Response Flow
378
+ ----------------------------------
357
379
358
- To review what you 've learned so far :
380
+ Here's what we 've learned s ofar :
359
381
360
- #. A client sends an HTTP request;
382
+ #. A client (e.g. a browser) sends an HTTP request;
361
383
#. Each request executes the same, single file (called a "front controller");
362
384
#. The front controller boots Symfony and passes the request information;
363
- #. The router matches the request URI to a specific route and returns
364
- information about the route, including the controller (usually a PHP method)
365
- that should be executed;
366
- #. The controller (PHP method) is executed: this is where *your * code creates
367
- and returns the appropriate ``Response `` object;
385
+ #. Internally, Symfony uses *routes * and *controllers * to create the Response for
386
+ the page (we'll learn about these soon!);
368
387
#. Symfony turns your ``Response `` object into the text headers and content
369
388
(i.e. the HTTP response), which are sent back to the client.
370
389
371
- Symfony provides a powerful set of tools for rapidly developing web applications
372
- without imposing on your application. Normal users can quickly start development
373
- by using a Symfony distribution, which provides a project skeleton with
374
- sensible defaults. For more advanced users, the sky is the limit.
375
-
376
390
.. _`xkcd` : http://xkcd.com/
377
391
.. _`XMLHttpRequest` : https://en.wikipedia.org/wiki/XMLHttpRequest
378
392
.. _`HTTP 1.1 RFC` : http://www.w3.org/Protocols/rfc2616/rfc2616.html
0 commit comments