Skip to content

Commit 4b73474

Browse files
committed
[book][http_fundamentals] Filling in some missing details about Request information, specifically the attributes property after feedback from Crell at http://groups.drupal.org/node/198538
1 parent 550b9d8 commit 4b73474

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

book/http_fundamentals.rst

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,21 @@ have all the request information at your fingertips::
226226

227227
// retrieve GET and POST variables respectively
228228
$request->query->get('foo');
229-
$request->request->get('bar');
229+
$request->request->get('bar', 'default value if bar does not exist');
230+
231+
// retrieve SERVER variables
232+
$request->server->get('HTTP_HOST');
230233

231234
// retrieves an instance of UploadedFile identified by foo
232235
$request->files->get('foo');
233236

237+
// retrieve a COOKIE value
238+
$request->cookies->get('PHPSESSID');
239+
240+
// retrieve an HTTP request header, with normalized, lowercase keys
241+
$request->headers->get('host');
242+
$request->headers->get('content_type');
243+
234244
$request->getMethod(); // GET, POST, PUT, DELETE, HEAD
235245
$request->getLanguages(); // an array of languages the client accepts
236246

@@ -239,6 +249,27 @@ you'll never need to worry about. For example, the ``isSecure()`` method
239249
checks the *three* different values in PHP that can indicate whether or not
240250
the user is connecting via a secured connection (i.e. ``https``).
241251

252+
.. sidebar:: ParameterBags and Request attributes
253+
254+
As seen above, the ``$_GET`` and ``$_POST`` variables are accessible via
255+
the public ``query`` and ``request`` properties respectively. Each of
256+
these objects is a :class:`Symfony\\Component\\HttpFoundation\\ParameterBag`
257+
object, which has methods like
258+
:method:`Symfony\\Component\\HttpFoundation\\ParameterBag::get`,
259+
:method:`Symfony\\Component\\HttpFoundation\\ParameterBag::has`,
260+
:method:`Symfony\\Component\\HttpFoundation\\ParameterBag::all` and more.
261+
In fact, every public property used in the previous example is some instance
262+
of the ParameterBag.
263+
264+
The Request class also has a public ``attributes`` property, which holds
265+
special data related to how the application works internally. For the
266+
Symfony2 framework, the ``attributes`` holds the values returned by the
267+
matched route, like ``_controller``, ``id`` (if you have an ``{id}``
268+
wildcard), and even the name of the matched route (``_route``). The
269+
``attributes`` property exists entirely to be a place where you can
270+
prepare and store context-specific information about the request.
271+
272+
242273
Symfony also provides a ``Response`` class: a simple PHP representation of
243274
an HTTP response message. This allows your application to use an object-oriented
244275
interface to construct the response that needs to be returned to the client::

0 commit comments

Comments
 (0)