Skip to content

Commit ce24865

Browse files
xabbuhjaviereguiluz
authored andcommitted
Creating the Controller topic
1 parent d213ad9 commit ce24865

File tree

7 files changed

+66
-70
lines changed

7 files changed

+66
-70
lines changed

book/index.rst

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ The Book
88
from_flat_php_to_symfony2
99
installation
1010
page_creation
11-
controller
1211
routing
1312
templating
1413
configuration

book/controller.rst renamed to controller.rst

-69
Original file line numberDiff line numberDiff line change
@@ -791,73 +791,6 @@ There are also special classes to make certain kinds of responses easier:
791791
``Request`` and ``Response`` object in the
792792
:ref:`HttpFoundation component documentation <component-http-foundation-request>`.
793793

794-
Creating Static Pages
795-
---------------------
796-
797-
You can create a static page without even creating a controller (only a route
798-
and template are needed). See cookbook article
799-
:doc:`/cookbook/templating/render_without_controller`.
800-
801-
.. index::
802-
single: Controller; Forwarding
803-
804-
Forwarding to Another Controller
805-
--------------------------------
806-
807-
Though not very common, you can also forward to another controller
808-
internally with the :method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::forward`
809-
method. Instead of redirecting the user's browser, this makes an "internal" sub-request
810-
and calls the defined controller. The ``forward()`` method returns the ``Response``
811-
object that's returned from *that* controller::
812-
813-
public function indexAction($name)
814-
{
815-
$response = $this->forward('AppBundle:Something:fancy', array(
816-
'name' => $name,
817-
'color' => 'green',
818-
));
819-
820-
// ... further modify the response or return it directly
821-
822-
return $response;
823-
}
824-
825-
The array passed to the method becomes the arguments for the resulting controller.
826-
The target controller method might look something like this::
827-
828-
public function fancyAction($name, $color)
829-
{
830-
// ... create and return a Response object
831-
}
832-
833-
Just like when creating a controller for a route, the order of the arguments of
834-
``fancyAction()`` doesn't matter: the matching is done by name.
835-
836-
.. _checking-the-validity-of-a-csrf-token:
837-
838-
Validating a CSRF Token
839-
-----------------------
840-
841-
Sometimes, you want to use CSRF protection in an action where you don't want to
842-
use the Symfony Form component. If, for example, you're doing a DELETE action,
843-
you can use the :method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::isCsrfTokenValid`
844-
method to check the CSRF token::
845-
846-
if ($this->isCsrfTokenValid('token_id', $submittedToken)) {
847-
// ... do something, like deleting an object
848-
}
849-
850-
.. versionadded:: 2.6
851-
The ``isCsrfTokenValid()`` shortcut method was introduced in Symfony 2.6.
852-
It is equivalent to executing the following code:
853-
854-
.. code-block:: php
855-
856-
use Symfony\Component\Security\Csrf\CsrfToken;
857-
858-
$this->get('security.csrf.token_manager')
859-
->isTokenValid(new CsrfToken('token_id', 'TOKEN'));
860-
861794
Final Thoughts
862795
--------------
863796

@@ -880,5 +813,3 @@ Learn more from the Cookbook
880813

881814
* :doc:`/cookbook/controller/error_pages`
882815
* :doc:`/cookbook/controller/service`
883-
884-
.. _`Controller class`: https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.. index::
2+
single: Controller; Validating CSRF Tokens
3+
4+
How to Manually Validate a CSRF Token in a Controller
5+
=====================================================
6+
7+
Sometimes, you want to use CSRF protection in an action where you do not
8+
want to use the Symfony Form component. If, for example, you are implementing
9+
a DELETE action, you can use the :method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::isCsrfTokenValid`
10+
method to check the validity of a CSRF token::
11+
12+
public function deleteAction()
13+
{
14+
if ($this->isCsrfTokenValid('token_id', $submittedToken)) {
15+
// ... do something, like deleting an object
16+
}
17+
}

cookbook/controller/forwarding.rst

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
.. index::
2+
single: Controller; Forwarding
3+
4+
How to Forward Requests to another Controller
5+
=============================================
6+
7+
Though not very common, you can also forward to another controller internally
8+
with the :method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::forward`
9+
method. Instead of redirecting the user's browser, this makes an "internal"
10+
sub-request and calls the defined controller. The ``forward()`` method returns
11+
the :class:`Symfony\Component\HttpFoundation\Response` object that is returned
12+
from *that* controller::
13+
14+
public function indexAction($name)
15+
{
16+
$response = $this->forward('AppBundle:Something:fancy', array(
17+
'name' => $name,
18+
'color' => 'green',
19+
));
20+
21+
// ... further modify the response or return it directly
22+
23+
return $response;
24+
}
25+
26+
The array passed to the method becomes the arguments for the resulting controller.
27+
The target controller method might look something like this::
28+
29+
public function fancyAction($name, $color)
30+
{
31+
// ... create and return a Response object
32+
}
33+
34+
Just like when creating a controller for a route, the order of the arguments
35+
of ``fancyAction()`` doesn't matter: the matching is done by name.

cookbook/controller/index.rst

+2
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ Controller
77
error_pages
88
service
99
upload_file
10+
forwarding
11+
csrf_token_validation

cookbook/map.rst.inc

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
* :doc:`/cookbook/controller/error_pages`
5656
* :doc:`/cookbook/controller/service`
5757
* :doc:`/cookbook/controller/upload_file`
58+
* :doc:`/cookbook/controller/forwarding`
59+
* :doc:`/cookbook/controller/csrf_token_validation`
5860

5961
* **Debugging**
6062

index.rst

+10
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ Get started fast with the Symfony :doc:`Quick Tour <quick_tour/index>`:
2323
* :doc:`quick_tour/the_controller` >
2424
* :doc:`quick_tour/the_architecture`
2525

26+
Topics
27+
------
28+
29+
.. toctree::
30+
:hidden:
31+
32+
controller
33+
34+
* :doc:`/controller`
35+
2636
Book
2737
----
2838

0 commit comments

Comments
 (0)