@@ -590,8 +590,6 @@ Accessing other Services
590
590
When extending the base controller class, you can access any Symfony2 service
591
591
via the ``get() `` method. Here are several common services you might need::
592
592
593
- $request = $this->getRequest();
594
-
595
593
$templating = $this->get('templating');
596
594
597
595
$router = $this->get('router');
@@ -660,16 +658,21 @@ by using the native PHP sessions.
660
658
Storing and retrieving information from the session can be easily achieved
661
659
from any controller::
662
660
663
- $session = $this->getRequest()->getSession();
661
+ use Symfony\Component\HttpFoundation\Request;
662
+
663
+ public function indexAction(Request $request)
664
+ {
665
+ $session = $request->getSession();
664
666
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');
667
669
668
- // in another controller for another request
669
- $foo = $session->get('foo');
670
+ // in another controller for another request
671
+ $foo = $session->get('foo');
670
672
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
+ }
673
676
674
677
These attributes will remain on the user for the remainder of that user's
675
678
session.
@@ -687,11 +690,13 @@ These types of messages are called "flash" messages.
687
690
688
691
For example, imagine you're processing a form submit::
689
692
690
- public function updateAction()
693
+ use Symfony\Component\HttpFoundation\Request;
694
+
695
+ public function updateAction(Request $request)
691
696
{
692
697
$form = $this->createForm(...);
693
698
694
- $form->handleRequest($this->getRequest() );
699
+ $form->handleRequest($request );
695
700
696
701
if ($form->isValid()) {
697
702
// do some sort of processing
@@ -783,17 +788,22 @@ The Request Object
783
788
------------------
784
789
785
790
Besides 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 `::
787
794
788
- $request = $this->getRequest() ;
795
+ use Symfony\Component\HttpFoundation\Request ;
789
796
790
- $request->isXmlHttpRequest(); // is it an Ajax request?
797
+ public function indexAction(Request $request)
798
+ {
799
+ $request->isXmlHttpRequest(); // is it an Ajax request?
791
800
792
- $request->getPreferredLanguage(array('en', 'fr'));
801
+ $request->getPreferredLanguage(array('en', 'fr'));
793
802
794
- $request->query->get('page'); // get a $_GET parameter
803
+ $request->query->get('page'); // get a $_GET parameter
795
804
796
- $request->request->get('page'); // get a $_POST parameter
805
+ $request->request->get('page'); // get a $_POST parameter
806
+ }
797
807
798
808
Like the ``Response `` object, the request headers are stored in a ``HeaderBag ``
799
809
object and are easily accessible.
0 commit comments