@@ -719,13 +719,53 @@ read any flash messages from the session:
719719.. index ::
720720 single: Controller; Response object
721721
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');
724744
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::
729769
730770 use Symfony\Component\HttpFoundation\Response;
731771
@@ -736,11 +776,6 @@ content that's sent back to the client::
736776 $response = new Response(json_encode(array('name' => $name)));
737777 $response->headers->set('Content-Type', 'application/json');
738778
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-
744779There are also special classes to make certain kinds of responses easier:
745780
746781* 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:
749784* For files, there is :class: `Symfony\\ Component\\ HttpFoundation\\ BinaryFileResponse `.
750785 See :ref: `component-http-foundation-serving-files `.
751786
752- * For streamed responses, there is :class: `Symfony\\ Component\\ HttpFoundation\\ StreamedResponse `.
787+ * For streamed responses, there is
788+ :class: `Symfony\\ Component\\ HttpFoundation\\ StreamedResponse `.
753789 See :ref: `streaming-response `.
754790
755791.. seealso ::
756792
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 >`.
791796
792797Creating Static Pages
793798---------------------
794799
795800You 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 `.
799803
800804.. index ::
801805 single: Controller; Forwarding
802806
803807Forwarding to Another Controller
804808--------------------------------
805809
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::
811819
812820 public function indexAction($name)
813821 {
@@ -821,36 +829,42 @@ object that's returned from *that* controller::
821829 return $response;
822830 }
823831
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::
831834
832835 public function fancyAction($name, $color)
833836 {
834837 // ... create and return a Response object
835838 }
836839
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.
842855
843- Checking the Validity of a CSRF Token
844- -------------------------------------
856+ Checking the Validity of a CSRF Token inside Controller
857+ -------------------------------------------------------
845858
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.
848861
849862If, 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 `
851864method to check the CSRF token::
852865
853866 $csrf = $this->container->get('form.csrf_provider');
867+
854868 $intention = 'authenticate';
855869 $token = $csrf->generateCsrfToken($intention);
856870
0 commit comments