Skip to content

Commit 8195f96

Browse files
committed
Proofing the new HTTP fundamentals
1) Putting a couple of things back that I (subjectively) like 2) Removing some parts that don't make sense anymore. For example, we removed pretty much all details about what a route & controller is (ok, we talk about it in following chapters). I removed a few spots that still talked about these
1 parent 8a6c30e commit 8195f96

File tree

4 files changed

+58
-39
lines changed

4 files changed

+58
-39
lines changed

components/http_foundation.rst

+2
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,8 @@ method::
427427
If the Response is not modified, it sets the status code to 304 and removes the
428428
actual response content.
429429

430+
.. _redirect-response:
431+
430432
Redirecting the User
431433
~~~~~~~~~~~~~~~~~~~~
432434

http_fundamentals.rst

+49-35
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
Symfony and HTTP Fundamentals
77
=============================
88

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.
1415

1516
Requests and Responses in HTTP
1617
------------------------------
@@ -261,6 +262,12 @@ needs to be returned to the client::
261262
// print the HTTP headers followed by the content
262263
$response->send();
263264

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+
264271
.. tip::
265272

266273
The ``Request`` and ``Response`` classes are part of a standalone component
@@ -314,27 +321,43 @@ handles every request coming into your application. For example:
314321
| ``/index.php/blog`` | executes ``index.php`` |
315322
+------------------------+------------------------+
316323

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
323325

324326
Now, every request is handled exactly the same way. Instead of individual URLs
325327
executing different PHP files, the front controller is *always* executed,
326328
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.
329351

330352
.. index::
331353
single: HTTP; Symfony request flow
332354

333355
The Symfony Application Flow
334356
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
335357

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:
338361

339362
.. _request-flow-figure:
340363

@@ -345,34 +368,25 @@ request. Symfony follows the same simple pattern for every request:
345368
Incoming requests are interpreted by the :doc:`Routing component </book/routing>` and
346369
passed to PHP functions that return ``Response`` objects.
347370

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.
354376

355-
Conclusion
356-
----------
377+
Summary: The Request-Response Flow
378+
----------------------------------
357379

358-
To review what you've learned so far:
380+
Here's what we've learned s ofar:
359381

360-
#. A client sends an HTTP request;
382+
#. A client (e.g. a browser) sends an HTTP request;
361383
#. Each request executes the same, single file (called a "front controller");
362384
#. 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!);
368387
#. Symfony turns your ``Response`` object into the text headers and content
369388
(i.e. the HTTP response), which are sent back to the client.
370389

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-
376390
.. _`xkcd`: http://xkcd.com/
377391
.. _`XMLHttpRequest`: https://en.wikipedia.org/wiki/XMLHttpRequest
378392
.. _`HTTP 1.1 RFC`: http://www.w3.org/Protocols/rfc2616/rfc2616.html

includes/_rewrite_rule_tip.rst.inc

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.. tip::
2+
3+
By using rewrite rules in your
4+
:doc:`web server configuration </cookbook/configuration/web_server_configuration>`,
5+
the ``index.php`` won't be needed and you will have beautiful, clean URLs
6+
(e.g. ``/show``).

introduction/from_flat_php_to_symfony2.rst

+1-4
Original file line numberDiff line numberDiff line change
@@ -336,10 +336,7 @@ application change slightly, but start to become more flexible:
336336
/index.php => Blog post list page (index.php executed)
337337
/index.php/show => Blog post show page (index.php executed)
338338
339-
.. tip::
340-
341-
By using rewrite rules in your :doc:`web server configuration </cookbook/configuration/web_server_configuration>`,
342-
the ``index.php`` won't be needed and you will have beautiful, clean URLs (e.g. ``/show``).
339+
.. include:: includes/_rewrite_rule_tip.rst.inc
343340

344341
When using a front controller, a single PHP file (``index.php`` in this case)
345342
renders *every* request. For the blog post show page, ``/index.php/show`` will

0 commit comments

Comments
 (0)