@@ -590,8 +590,6 @@ Accessing other Services
590590When extending the base controller class, you can access any Symfony2 service
591591via the ``get() `` method. Here are several common services you might need::
592592
593- $request = $this->getRequest();
594-
595593 $templating = $this->get('templating');
596594
597595 $router = $this->get('router');
@@ -660,16 +658,21 @@ by using the native PHP sessions.
660658Storing and retrieving information from the session can be easily achieved
661659from any controller::
662660
663- $session = $this->getRequest()->getSession();
661+ use Symfony\Component\HttpFoundation\Request;
662+
663+ public function indexAction(Request $request)
664+ {
665+ $session = $request->getSession();
664666
665- // store an attribute for reuse during a later user request
666- $session->set('foo', 'bar');
667+ // store an attribute for reuse during a later user request
668+ $session->set('foo', 'bar');
667669
668- // in another controller for another request
669- $foo = $session->get('foo');
670+ // in another controller for another request
671+ $foo = $session->get('foo');
670672
671- // use a default value if the key doesn't exist
672- $filters = $session->get('filters', array());
673+ // use a default value if the key doesn't exist
674+ $filters = $session->get('filters', array());
675+ }
673676
674677These attributes will remain on the user for the remainder of that user's
675678session.
@@ -687,11 +690,13 @@ These types of messages are called "flash" messages.
687690
688691For example, imagine you're processing a form submit::
689692
690- public function updateAction()
693+ use Symfony\Component\HttpFoundation\Request;
694+
695+ public function updateAction(Request $request)
691696 {
692697 $form = $this->createForm(...);
693698
694- $form->handleRequest($this->getRequest() );
699+ $form->handleRequest($request );
695700
696701 if ($form->isValid()) {
697702 // do some sort of processing
@@ -783,17 +788,22 @@ The Request Object
783788------------------
784789
785790Besides the values of the routing placeholders, the controller also has access
786- to the ``Request `` object when extending the base ``Controller `` class::
791+ to the ``Request `` object. The framework injects the ``Request `` object in the
792+ controller if a variable is type-hinted with
793+ `Symfony\Component\HttpFoundation\Request `::
787794
788- $request = $this->getRequest() ;
795+ use Symfony\Component\HttpFoundation\Request ;
789796
790- $request->isXmlHttpRequest(); // is it an Ajax request?
797+ public function indexAction(Request $request)
798+ {
799+ $request->isXmlHttpRequest(); // is it an Ajax request?
791800
792- $request->getPreferredLanguage(array('en', 'fr'));
801+ $request->getPreferredLanguage(array('en', 'fr'));
793802
794- $request->query->get('page'); // get a $_GET parameter
803+ $request->query->get('page'); // get a $_GET parameter
795804
796- $request->request->get('page'); // get a $_POST parameter
805+ $request->request->get('page'); // get a $_POST parameter
806+ }
797807
798808Like the ``Response `` object, the request headers are stored in a ``HeaderBag ``
799809object and are easily accessible.
0 commit comments