Skip to content

Commit 636d20b

Browse files
committed
Merge pull request #3330 from lyrixx/request-2.3
Removed all occurences of $this->getRequest()
2 parents eb2c184 + 41178bc commit 636d20b

File tree

6 files changed

+77
-45
lines changed

6 files changed

+77
-45
lines changed

book/controller.rst

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -496,9 +496,9 @@ value to each variable.
496496
directly by duplicating the current request. When this
497497
:ref:`sub request <http-kernel-sub-requests>` is executed via the ``http_kernel``
498498
service the ``HttpKernel`` returns a ``Response`` object::
499-
499+
500500
use Symfony\Component\HttpKernel\HttpKernelInterface;
501-
501+
502502
$path = array(
503503
'_controller' => 'AcmeHelloBundle:Hello:fancy',
504504
'name' => $name,
@@ -585,8 +585,6 @@ Accessing other Services
585585
When extending the base controller class, you can access any Symfony2 service
586586
via the ``get()`` method. Here are several common services you might need::
587587

588-
$request = $this->getRequest();
589-
590588
$templating = $this->get('templating');
591589

592590
$router = $this->get('router');
@@ -655,16 +653,21 @@ by using the native PHP sessions.
655653
Storing and retrieving information from the session can be easily achieved
656654
from any controller::
657655

658-
$session = $this->getRequest()->getSession();
656+
use Symfony\Component\HttpFoundation\Response;
659657

660-
// store an attribute for reuse during a later user request
661-
$session->set('foo', 'bar');
658+
public function indexAction(Request $request)
659+
{
660+
$session = $request->getSession();
661+
662+
// store an attribute for reuse during a later user request
663+
$session->set('foo', 'bar');
662664

663-
// in another controller for another request
664-
$foo = $session->get('foo');
665+
// in another controller for another request
666+
$foo = $session->get('foo');
665667

666-
// use a default value if the key doesn't exist
667-
$filters = $session->get('filters', array());
668+
// use a default value if the key doesn't exist
669+
$filters = $session->get('filters', array());
670+
}
668671

669672
These attributes will remain on the user for the remainder of that user's
670673
session.
@@ -682,11 +685,13 @@ These types of messages are called "flash" messages.
682685

683686
For example, imagine you're processing a form submit::
684687

685-
public function updateAction()
688+
use Symfony\Component\HttpFoundation\Request;
689+
690+
public function updateAction(Request $request)
686691
{
687692
$form = $this->createForm(...);
688693

689-
$form->handleRequest($this->getRequest());
694+
$form->handleRequest($request);
690695

691696
if ($form->isValid()) {
692697
// do some sort of processing
@@ -775,17 +780,22 @@ The Request Object
775780
------------------
776781

777782
Besides the values of the routing placeholders, the controller also has access
778-
to the ``Request`` object when extending the base ``Controller`` class::
783+
to the ``Request`` object. The framework injects the ``Request`` object in the
784+
controller if a variable is type hinted with
785+
`Symfony\Component\HttpFoundation\Request`::
779786

780-
$request = $this->getRequest();
787+
use Symfony\Component\HttpFoundation\Request;
781788

782-
$request->isXmlHttpRequest(); // is it an Ajax request?
789+
public function indexAction(Request $request)
790+
{
791+
$request->isXmlHttpRequest(); // is it an Ajax request?
783792

784-
$request->getPreferredLanguage(array('en', 'fr'));
793+
$request->getPreferredLanguage(array('en', 'fr'));
785794

786-
$request->query->get('page'); // get a $_GET parameter
795+
$request->query->get('page'); // get a $_GET parameter
787796

788-
$request->request->get('page'); // get a $_POST parameter
797+
$request->request->get('page'); // get a $_POST parameter
798+
}
789799

790800
Like the ``Response`` object, the request headers are stored in a ``HeaderBag``
791801
object and are easily accessible.

book/http_cache.rst

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -557,12 +557,14 @@ each ``ETag`` must be unique across all representations of the same resource.
557557

558558
To see a simple implementation, generate the ETag as the md5 of the content::
559559

560-
public function indexAction()
560+
use Symfony\Component\HttpFoundation\Request;
561+
562+
public function indexAction(Request $request)
561563
{
562564
$response = $this->render('MyBundle:Main:index.html.twig');
563565
$response->setETag(md5($response->getContent()));
564566
$response->setPublic(); // make sure the response is public/cacheable
565-
$response->isNotModified($this->getRequest());
567+
$response->isNotModified($request);
566568

567569
return $response;
568570
}
@@ -604,7 +606,9 @@ For instance, you can use the latest update date for all the objects needed to
604606
compute the resource representation as the value for the ``Last-Modified``
605607
header value::
606608

607-
public function showAction($articleSlug)
609+
use Symfony\Component\HttpFoundation\Request;
610+
611+
public function showAction($articleSlug, Request $request)
608612
{
609613
// ...
610614

@@ -617,7 +621,7 @@ header value::
617621
// Set response as public. Otherwise it will be private by default.
618622
$response->setPublic();
619623

620-
if ($response->isNotModified($this->getRequest())) {
624+
if ($response->isNotModified($request)) {
621625
return $response;
622626
}
623627

@@ -653,8 +657,9 @@ the better. The ``Response::isNotModified()`` method does exactly that by
653657
exposing a simple and efficient pattern::
654658

655659
use Symfony\Component\HttpFoundation\Response;
660+
use Symfony\Component\HttpFoundation\Request;
656661

657-
public function showAction($articleSlug)
662+
public function showAction($articleSlug, Request $request)
658663
{
659664
// Get the minimum information to compute
660665
// the ETag or the Last-Modified value
@@ -671,7 +676,7 @@ exposing a simple and efficient pattern::
671676
$response->setPublic();
672677

673678
// Check that the Response is not modified for the given Request
674-
if ($response->isNotModified($this->getRequest())) {
679+
if ($response->isNotModified($request)) {
675680
// return the 304 Response immediately
676681
return $response;
677682
} else {

book/security.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,13 +429,13 @@ Next, create the controller that will display the login form::
429429
namespace Acme\SecurityBundle\Controller;
430430

431431
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
432+
use Symfony\Component\HttpFoundation\Request;
432433
use Symfony\Component\Security\Core\SecurityContext;
433434

434435
class SecurityController extends Controller
435436
{
436-
public function loginAction()
437+
public function loginAction(Request $request)
437438
{
438-
$request = $this->getRequest();
439439
$session = $request->getSession();
440440

441441
// get the login error if there is one

book/translation.rst

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -411,12 +411,14 @@ Handling the User's Locale
411411
The locale of the current user is stored in the request and is accessible
412412
via the ``request`` object::
413413

414-
// access the request object in a standard controller
415-
$request = $this->getRequest();
414+
use Symfony\Component\HttpFoundation\Request;
416415

417-
$locale = $request->getLocale();
416+
public function indexAction(Request $request)
417+
{
418+
$locale = $request->getLocale();
418419

419-
$request->setLocale('en_US');
420+
$request->setLocale('en_US');
421+
}
420422

421423
.. tip::
422424

cookbook/session/locale_sticky_session.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,9 @@ use the :method:`Request::getLocale <Symfony\\Component\\HttpFoundation\\Request
100100
method::
101101

102102
// from a controller...
103-
$locale = $this->getRequest()->getLocale();
103+
use Symfony\Component\HttpFoundation\Request;
104+
105+
public function indexAction(Request $request)
106+
{
107+
$locale = $request->getLocale();
108+
}

quick_tour/the_controller.rst

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,22 @@ Getting information from the Request
9090
------------------------------------
9191

9292
Besides the values of the routing placeholders, the controller also has access
93-
to the ``Request`` object::
93+
to the ``Request`` object. The framework injects the ``Request`` object in the
94+
controller if a variable is type hinted with
95+
`Symfony\Component\HttpFoundation\Request`::
9496

95-
$request = $this->getRequest();
97+
use Symfony\Component\HttpFoundation\Request;
9698

97-
$request->isXmlHttpRequest(); // is it an Ajax request?
99+
public function indexAction(Request $request)
100+
{
101+
$request->isXmlHttpRequest(); // is it an Ajax request?
98102

99-
$request->getPreferredLanguage(array('en', 'fr'));
103+
$request->getPreferredLanguage(array('en', 'fr'));
100104

101-
$request->query->get('page'); // get a $_GET parameter
105+
$request->query->get('page'); // get a $_GET parameter
102106

103-
$request->request->get('page'); // get a $_POST parameter
107+
$request->request->get('page'); // get a $_POST parameter
108+
}
104109

105110
In a template, you can also access the ``Request`` object via the
106111
``app.request`` variable:
@@ -122,16 +127,21 @@ by using native PHP sessions.
122127
Storing and retrieving information from the session can be easily achieved
123128
from any controller::
124129

125-
$session = $this->getRequest()->getSession();
130+
use Symfony\Component\HttpFoundation\Request;
126131

127-
// store an attribute for reuse during a later user request
128-
$session->set('foo', 'bar');
132+
public function indexAction(Request $request)
133+
{
134+
$session = $this->request->getSession();
129135

130-
// in another controller for another request
131-
$foo = $session->get('foo');
136+
// store an attribute for reuse during a later user request
137+
$session->set('foo', 'bar');
132138

133-
// use a default value if the key doesn't exist
134-
$filters = $session->get('filters', array());
139+
// in another controller for another request
140+
$foo = $session->get('foo');
141+
142+
// use a default value if the key doesn't exist
143+
$filters = $session->get('filters', array());
144+
}
135145

136146
You can also store small messages that will only be available for the very
137147
next request::

0 commit comments

Comments
 (0)