@@ -719,13 +719,53 @@ read any flash messages from the session:
719
719
.. index ::
720
720
single: Controller; Response object
721
721
722
- The Response Object
723
- -------------------
722
+ The Request and Response Object
723
+ -------------------------------
724
+
725
+ As already mentioned a :ref: `little earlier <book-controller-request-argument >`,
726
+ besides the values of the routing parameters, the controller has also access
727
+ to the ``Request `` object. The framework injects the ``Request `` object
728
+ in the controller if a variable is type-hinted with ``Request `` class::
729
+
730
+ use Symfony\Component\HttpFoundation\Request;
731
+
732
+ public function indexAction(Request $request)
733
+ {
734
+ $request->isXmlHttpRequest(); // is it an Ajax request?
735
+
736
+ $request->getPreferredLanguage(array('en', 'fr'));
737
+
738
+ // retrieve GET and POST variables respectively
739
+ $request->query->get('page');
740
+ $request->request->get('page');
741
+
742
+ // retrieve SERVER variables
743
+ $request->server->get('HTTP_HOST');
724
744
725
- The only requirement for a controller is to return a ``Response `` object. The
726
- :class: `Symfony\\ Component\\ HttpFoundation\\ Response ` class is an abstraction
727
- around the HTTP response: the text-based message filled with headers and
728
- content that's sent back to the client::
745
+ // retrieves an instance of UploadedFile identified by foo
746
+ $request->files->get('foo');
747
+
748
+ // retrieve a COOKIE value
749
+ $request->cookies->get('PHPSESSID');
750
+
751
+ // retrieve an HTTP request header, with normalized, lowercase keys
752
+ $request->headers->get('host');
753
+ $request->headers->get('content_type');
754
+ }
755
+
756
+ ``Request `` class has several public properties via which information about the client
757
+ request can be accessed.
758
+
759
+ Like the ``Request ``, the ``Response `` object has also a public ``headers `` property
760
+ which is a :class: `Symfony\\ Component\\ HttpFoundation\\ ResponseHeaderBag ` instance.
761
+ ``ResponseHeaderBag `` instances have methods for getting and setting the response
762
+ headers. The header names are normalized so that using ``Content-Type `` is equivalent
763
+ to ``content-type `` or even ``content_type ``.
764
+
765
+ The only requirement for a controller is to return a ``Response `` object.
766
+ The :class: `Symfony\\ Component\\ HttpFoundation\\ Response ` class is an
767
+ abstraction around the HTTP response - the text-based message filled with
768
+ headers and content that's sent back to the client::
729
769
730
770
use Symfony\Component\HttpFoundation\Response;
731
771
@@ -736,11 +776,6 @@ content that's sent back to the client::
736
776
$response = new Response(json_encode(array('name' => $name)));
737
777
$response->headers->set('Content-Type', 'application/json');
738
778
739
- The ``headers `` property is a :class: `Symfony\\ Component\\ HttpFoundation\\ HeaderBag `
740
- object and has some nice methods for getting and setting the headers. The
741
- header names are normalized so that using ``Content-Type `` is equivalent to
742
- ``content-type `` or even ``content_type ``.
743
-
744
779
There are also special classes to make certain kinds of responses easier:
745
780
746
781
* For JSON, there is :class: `Symfony\\ Component\\ HttpFoundation\\ JsonResponse `.
@@ -749,65 +784,38 @@ There are also special classes to make certain kinds of responses easier:
749
784
* For files, there is :class: `Symfony\\ Component\\ HttpFoundation\\ BinaryFileResponse `.
750
785
See :ref: `component-http-foundation-serving-files `.
751
786
752
- * For streamed responses, there is :class: `Symfony\\ Component\\ HttpFoundation\\ StreamedResponse `.
787
+ * For streamed responses, there is
788
+ :class: `Symfony\\ Component\\ HttpFoundation\\ StreamedResponse `.
753
789
See :ref: `streaming-response `.
754
790
755
791
.. seealso ::
756
792
757
- Don't worry! There is a lot more information about the Response object
758
- in the component documentation. See :ref: `component-http-foundation-response `.
759
-
760
- .. index ::
761
- single: Controller; Request object
762
-
763
- The Request Object
764
- ------------------
765
-
766
- Besides the values of the routing placeholders, the controller also has access
767
- to the ``Request `` object. The framework injects the ``Request `` object in the
768
- controller if a variable is type-hinted with
769
- :class: `Symfony\\ Component\\ HttpFoundation\\ Request `::
770
-
771
- use Symfony\Component\HttpFoundation\Request;
772
-
773
- public function indexAction(Request $request)
774
- {
775
- $request->isXmlHttpRequest(); // is it an Ajax request?
776
-
777
- $request->getPreferredLanguage(array('en', 'fr'));
778
-
779
- $request->query->get('page'); // get a $_GET parameter
780
-
781
- $request->request->get('page'); // get a $_POST parameter
782
- }
783
-
784
- Like the ``Response `` object, the request headers are stored in a ``HeaderBag ``
785
- object and are easily accessible.
786
-
787
- .. seealso ::
788
-
789
- Don't worry! There is a lot more information about the Request object
790
- in the component documentation. See :ref: `component-http-foundation-request `.
793
+ Now that you know the basics you can continue your research on Symfony
794
+ ``Request `` and ``Response `` object in the
795
+ :ref: `HttpFoundation component documentation <component-http-foundation-request >`.
791
796
792
797
Creating Static Pages
793
798
---------------------
794
799
795
800
You can create a static page without even creating a controller (only a route
796
- and template are needed).
797
-
798
- See :doc: `/cookbook/templating/render_without_controller `.
801
+ and template are needed). See cookbook article
802
+ :doc: `/cookbook/templating/render_without_controller `.
799
803
800
804
.. index ::
801
805
single: Controller; Forwarding
802
806
803
807
Forwarding to Another Controller
804
808
--------------------------------
805
809
806
- Though not very common, you can also forward to another controller internally
807
- with the :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::forward `
808
- method. Instead of redirecting the user's browser, it makes an internal sub-request,
809
- and calls the controller. The ``forward() `` method returns the ``Response ``
810
- object that's returned from *that * controller::
810
+ We already saw how to redirect the :ref: `user's browser <book-redirecting-users-browser >`
811
+ to another page internally or to some external URL.
812
+
813
+ Though not very common, you can also forward to another controller
814
+ internally with the basic ``Controller `` class method
815
+ :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::forward `.
816
+ Instead of redirecting the user's browser, method makes an internal
817
+ sub-request, and calls the defined controller. The ``forward() `` method returns
818
+ the ``Response `` object that's returned from *that * controller::
811
819
812
820
public function indexAction($name)
813
821
{
@@ -821,36 +829,42 @@ object that's returned from *that* controller::
821
829
return $response;
822
830
}
823
831
824
- Notice that the ``forward() `` method uses a special string representation
825
- of the controller (see :ref: `controller-string-syntax `). In this case, the
826
- target controller function will be ``SomethingController::fancyAction() ``
827
- inside the AppBundle. The array passed to the method becomes the arguments on
828
- the resulting controller. This same idea is used when embedding controllers
829
- into templates (see :ref: `templating-embedding-controller `). The target
830
- controller method would look something like this::
832
+ The array passed to the method becomes the arguments on the resulting controller.
833
+ The target controller method would look something like this::
831
834
832
835
public function fancyAction($name, $color)
833
836
{
834
837
// ... create and return a Response object
835
838
}
836
839
837
- Just like when creating a controller for a route, the order of the arguments of
838
- ``fancyAction `` doesn't matter. Symfony matches the index key names (e.g.
839
- ``name ``) with the method argument names (e.g. ``$name ``). If you change the
840
- order of the arguments, Symfony will still pass the correct value to each
841
- variable.
840
+ .. sidebar :: Logical controller name
841
+
842
+ Notice that the ``forward() `` method uses a special string representation
843
+ called *logical controller name * which, for example, looks like
844
+ ``AppBundle:Hello:index ``. For more details on the controller format, read
845
+ :ref: `controller-string-syntax ` subtitle of the Routing chapter.
846
+
847
+ You can learn much more about the routing system in the
848
+ :doc: `Routing chapter </book/routing >`.
849
+
850
+ Just like when creating a controller for a route, the order of the
851
+ arguments of ``fancyAction() `` doesn't matter. Symfony matches the route
852
+ placeholder names (e.g. ``{name} ``) with the method argument names (e.g. ``$name ``).
853
+ If you change the order of the arguments, Symfony will still pass the correct
854
+ value to each variable.
842
855
843
- Checking the Validity of a CSRF Token
844
- -------------------------------------
856
+ Checking the Validity of a CSRF Token inside Controller
857
+ -------------------------------------------------------
845
858
846
- Sometimes you want to use CSRF protection in an action where you don't want to use a
847
- Symfony form.
859
+ Sometimes you want to use :ref: ` CSRF protection < forms-csrf >` in a controller where
860
+ you don't want to use a Symfony form.
848
861
849
862
If, for example, you're doing a DELETE action, you can use the
850
- :method: `Symfony\\ Component\\ Form\\ Extension\\ Csrf\\ CsrfProvider\\ CsrfProviderInterface ::isCsrfTokenValid `
863
+ :method: `Symfony\\ Component\\ Form\\ Extension\\ Csrf\\ CsrfProvider\\ SessionCsrfProvider ::isCsrfTokenValid `
851
864
method to check the CSRF token::
852
865
853
866
$csrf = $this->container->get('form.csrf_provider');
867
+
854
868
$intention = 'authenticate';
855
869
$token = $csrf->generateCsrfToken($intention);
856
870
0 commit comments