8000 Merge pull request #378 from hhamon/controller_tweak · phreaknerd/symfony-docs@1389e48 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1389e48

Browse files
committed
Merge pull request symfony#378 from hhamon/controller_tweak
[book] tweaked the controllers chapter.
2 parents a5fd504 + 2121b39 commit 1389e48

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

book/controller.rst

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ The controller for this can take several arguments::
246246
// ...
247247
}
248248

249-
Notice that both placeholder variables (``{{first_name}}``, ``{{last_name}}``)
249+
Notice that both placeholder variables (``{first_name}``, ``{last_name}``)
250250
as well as the default ``color`` variable are available as arguments in the
251251
controller. When a route is matched, the placeholder variables are merged
252252
with the ``defaults`` to make one array that's available to your controller.
@@ -395,7 +395,7 @@ perform a 301 (permanent) redirect, modify the second argument::
395395
.. tip::
396396

397397
The ``redirect()`` method is simply a shortcut that creates a ``Response``
398-
object that specializes in redirecting the user. It's equivalent to:
398+
object that specializes in redirecting the user. It's equivalent to::
399399
400400
use Symfony\Component\HttpFoundation\RedirectResponse;
401401
@@ -502,7 +502,7 @@ Accessing other Services
502502
When extending the base controller class, you can access any Symfony2 service
503503
via the ``get()`` method. Here are several common services you might need::
504504

505-
$request = $this->get('request');
505+
$request = $this->getRequest();
506506

507507
$response = $this->get('response');
508508

@@ -574,7 +574,7 @@ by using the native PHP sessions.
574574
Storing and retrieving information from the session can be easily achieved
575575
from any controller::
576576

577-
$session = $this->get('request')->getSession();
577+
$session = $this->getRequest()->getSession();
578578

579579
// store an attribute for reuse during a later user request
580580
$session->set('foo', 'bar');
@@ -603,7 +603,7 @@ Let's show an example where we're processing a form submit::
603603

604604
public function updateAction()
605605
{
606-
if ('POST' === $this->get('request')->getMethod()) {
606+
if ('POST' === $this->getRequest()->getMethod()) {
607607
// do some sort of processing
608608
609609
$this->get('session')->setFlash('notice', 'Your changes were saved!');
@@ -675,7 +675,7 @@ The Request Object
675675
Besides the values of the routing placeholders, the controller also has access
676676
to the ``Request`` object when extending the base ``Controller`` class::
677677

678-
$request = $this->get('request');
678+
$request = $this->getRequest();
679679

680680
$request->isXmlHttpRequest(); // is it an Ajax request?
681681

@@ -688,6 +688,17 @@ to the ``Request`` object when extending the base ``Controller`` class::
688688
Like the ``Response`` object, the request headers are stored in a ``HeaderBag``
689689
object and are easily accessible.
690690

691+
.. tip::
692+
693+
The ``Request`` object can also be injected as a parameter of an *action*::
694+
695+
use Symfony\Component\HttpFoundation\Request;
696+
697+
public function indexAction(Request $request)
698+
{
699+
$name = $request->cookies->get('name');
700+
}
701+
691702
.. index::
692703
single: Controller; Overview
693704

0 commit comments

Comments
 (0)
0