4
4
Controller
5
5
==========
6
6
7
- A controller is a PHP function you create that reads information from the Symfony's
7
+ A controller is a PHP function you create that reads information from the
8
8
``Request `` object and creates and returns a ``Response `` object. The response could
9
9
be an HTML page, JSON, XML, a file download, a redirect, a 404 error or anything
10
10
else you can dream up. The controller executes whatever arbitrary logic
11
11
*your application * needs to render the content of a page.
12
12
13
- See how simple this is by looking at a Symfony controller in action.
14
- This renders a page that prints a lucky (random) number::
15
-
16
- // src/Controller/LuckyController.php
17
- namespace App\Controller;
18
-
19
- use Symfony\Component\HttpFoundation\Response;
20
- use Symfony\Component\Routing\Annotation\Route;
21
-
22
- class LuckyController
23
- {
24
- /**
25
- * @Route("/lucky/number")
26
- */
27
- public function numberAction()
28
- {
29
- $number = mt_rand(0, 100);
30
-
31
- return new Response(
32
- '<html><body>Lucky number: '.$number.'</body></html>'
33
- );
34
- }
35
- }
36
-
37
- But in the real world, your controller will probably do a lot of work in order to
38
- create the response. It might read information from the request, load data from a
39
- database (or API), send an email or set information on the user's session.
40
- But in all cases, the controller will eventually return the ``Response `` object
41
- that will be delivered back to the client.
42
-
43
13
.. tip ::
44
14
45
15
If you haven't already created your first working page, check out
@@ -64,7 +34,7 @@ class::
64
34
class LuckyController
65
35
{
66
36
/**
67
- * @Route("/lucky/number/{max}")
37
+ * @Route("/lucky/number/{max}", name="app_lucky_number" )
68
38
*/
69
39
public function number($max)
70
40
{
@@ -126,14 +96,17 @@ To make life nicer, Symfony comes with two optional base
126
96
You can extend either to get access to some `helper methods `_.
127
97
128
98
Add the ``use `` statement atop your controller class and then modify
129
- ``LuckyController `` to extend it::
99
+ ``LuckyController `` to extend it:
100
+
101
+ .. code-block :: diff
130
102
131
103
// src/Controller/LuckyController.php
132
104
namespace App\Controller;
133
105
134
- use Symfony\Bundle\FrameworkBundle\Controller\Controller;
106
+ + use Symfony\Bundle\FrameworkBundle\Controller\Controller;
135
107
136
- class LuckyController extends Controller
108
+ - class LuckyController
109
+ + class LuckyController extends Controller
137
110
{
138
111
// ...
139
112
}
@@ -161,7 +134,7 @@ Generating URLs
161
134
The :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::generateUrl `
162
135
method is just a helper method that generates the URL for a given route::
163
136
164
- $url = $this->generateUrl('blog_show ', array('slug ' => 'slug-value' ));
137
+ $url = $this->generateUrl('app_lucky_number ', array('max ' => 10 ));
165
138
166
139
Redirecting
167
140
~~~~~~~~~~~
@@ -184,18 +157,16 @@ and ``redirect()`` methods::
184
157
return $this->redirectToRoute('homepage', array(), 301);
185
158
186
159
// redirect to a route with parameters
187
- return $this->redirectToRoute('blog_show ', array('slug ' => 'my-page' ));
160
+ return $this->redirectToRoute('app_lucky_number ', array('max ' => 10 ));
188
161
189
162
// redirect externally
190
163
return $this->redirect('http://symfony.com/doc');
191
164
}
192
165
193
- For more information, see the :doc: `Routing article </routing >`.
194
-
195
166
.. caution ::
196
167
197
168
The ``redirect() `` method does not check its destination in any way. If you
198
- redirect to some URL provided byend -users, your application may be open
169
+ redirect to a URL provided by end -users, your application may be open
199
170
to the `unvalidated redirects security vulnerability `_.
200
171
201
172
.. index ::
0 commit comments