@@ -66,7 +66,7 @@ class::
66
66
/**
67
67
* @Route("/lucky/number/{max}")
68
68
*/
69
- public function numberAction ($max)
69
+ public function number ($max)
70
70
{
71
71
$number = mt_rand(0, $max);
72
72
@@ -76,7 +76,7 @@ class::
76
76
}
77
77
}
78
78
79
- The controller is the ``numberAction () `` method, which lives inside a
79
+ The controller is the ``number () `` method, which lives inside a
80
80
controller class ``LuckyController ``.
81
81
82
82
This controller is pretty straightforward:
@@ -89,12 +89,10 @@ This controller is pretty straightforward:
89
89
must return.
90
90
91
91
* *line 7 *: The class can technically be called anything - but should end in the
92
- word ``Controller `` (this isn't * required *, but some shortcuts rely on this).
92
+ word ``Controller ``
93
93
94
- * *line 12 *: Each action method in a controller class is suffixed with ``Action ``
95
- (again, this isn't *required *, but some shortcuts rely on this). This method
96
- is allowed to have a ``$max `` argument thanks to the ``{max} ``
97
- :doc: `wildcard in the route </routing >`.
94
+ * *line 12 *: The action method is allowed to have a ``$max `` argument thanks to the
95
+ ``{max} `` :doc: `wildcard in the route </routing >`.
98
96
99
97
* *line 16 *: The controller creates and returns a ``Response `` object.
100
98
@@ -105,7 +103,8 @@ Mapping a URL to a Controller
105
103
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
106
104
107
105
In order to *view * the result of this controller, you need to map a URL to it via
108
- a route. This was done above with the ``@Route("/lucky/number/{max}") `` annotation.
106
+ a route. This was done above with the ``@Route("/lucky/number/{max}") ``
107
+ :ref: `route annotation <annotation-routes >`.
109
108
110
109
To see your page, go to this URL in your browser:
111
110
@@ -121,12 +120,12 @@ For more information on routing, see :doc:`/routing`.
121
120
The Base Controller Classes & Services
122
121
--------------------------------------
123
122
124
- For convenience , Symfony comes with two optional base
123
+ To make life nicer , Symfony comes with two optional base
125
124
:class: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller ` and
126
- :class: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ AbstractController `
127
- classes. You can extend either to get access to a number of `helper methods `_.
125
+ :class: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ AbstractController `.
126
+ You can extend either to get access to some `helper methods `_.
128
127
129
- Add the ``use `` statement atop the `` Controller `` class and then modify
128
+ Add the ``use `` statement atop your controller class and then modify
130
129
``LuckyController `` to extend it::
131
130
132
131
// src/Controller/LuckyController.php
@@ -146,11 +145,12 @@ and many others that you'll learn about next.
146
145
147
146
.. tip ::
148
147
149
- You can extend either ``Controller `` or ``AbstractController ``. The difference
150
- is that when you extend ``AbstractController ``, you can't access services directly
151
- via ``$this->get() `` or ``$this->container->get() ``. This forces you to write
152
- more robust code to access services. But if you *do * need direct access to the
153
- container, using ``Controller `` is fine.
148
+ What's the difference between ``Controller `` or ``AbstractController ``. Not much:
149
+ both are identical, except that ``AbstractController `` is more restrictive: it
150
+ does not allow you to access services directly via ``$this->get() `` or
151
+ ``$this->container->get() ``. This forces you to write more robust code to access
152
+ services. But if you *do * need direct access to the container, using ``Controller ``
153
+ is fine.
154
154
155
155
.. index ::
156
156
single: Controller; Redirecting
@@ -169,11 +169,17 @@ Redirecting
169
169
If you want to redirect the user to another page, use the ``redirectToRoute() ``
170
170
and ``redirect() `` methods::
171
171
172
+ use Symfony\Component\HttpFoundation\RedirectResponse;
173
+
174
+ // ...
172
175
public function indexAction()
173
176
{
174
177
// redirect to the "homepage" route
175
178
return $this->redirectToRoute('homepage');
176
179
180
+ // redirectToRoute is a shortcut for:
181
+ // return new RedirectResponse($this->generateUrl('homepage'));
182
+
177
183
// do a permanent - 301 redirect
178
184
return $this->redirectToRoute('homepage', array(), 301);
179
185
@@ -189,23 +195,9 @@ For more information, see the :doc:`Routing article </routing>`.
189
195
.. caution ::
190
196
191
197
The ``redirect() `` method does not check its destination in any way. If you
192
- redirect to some URL provided by the end -users, your application may be open
198
+ redirect to some URL provided byend -users, your application may be open
193
199
to the `unvalidated redirects security vulnerability `_.
194
200
195
-
196
- .. tip ::
197
-
198
- The ``redirectToRoute() `` method is simply a shortcut that creates a
199
- ``Response `` object that specializes in redirecting the user. It's
200
- equivalent to::
201
-
202
- use Symfony\Component\HttpFoundation\RedirectResponse;
203
-
204
- public function indexAction()
205
- {
206
- return new RedirectResponse($this->generateUrl('homepage'));
207
- }
208
-
209
201
.. index ::
210
202
single: Controller; Rendering templates
211
203
@@ -221,15 +213,7 @@ object for you::
221
213
// renders templates/lucky/number.html.twig
222
214
return $this->render('lucky/number.html.twig', array('name' => $name));
223
215
224
- Templates can also live in deeper sub-directories. Just try to avoid
225
- creating unnecessarily deep structures::
226
-
227
- // renders templates/lottery/lucky/number.html.twig
228
- return $this->render('lottery/lucky/number.html.twig', array(
229
- 'name' => $name,
230
- ));
231
-
232
- The Symfony templating system and Twig are explained more in the
216
+ Templating and Twig are explained more in the
233
217
:doc: `Creating and Using Templates article </templating >`.
234
218
235
219
.. index ::
0 commit comments