44Controller
55==========
66
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
88``Request `` object and creates and returns a ``Response `` object. The response could
99be an HTML page, JSON, XML, a file download, a redirect, a 404 error or anything
1010else you can dream up. The controller executes whatever arbitrary logic
1111*your application * needs to render the content of a page.
1212
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-
4313.. tip ::
4414
4515 If you haven't already created your first working page, check out
@@ -64,7 +34,7 @@ class::
6434 class LuckyController
6535 {
6636 /**
67- * @Route("/lucky/number/{max}")
37+ * @Route("/lucky/number/{max}", name="app_lucky_number" )
6838 */
6939 public function number($max)
7040 {
@@ -126,14 +96,17 @@ To make life nicer, Symfony comes with two optional base
12696You can extend either to get access to some `helper methods `_.
12797
12898Add 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
130102
131103 // src/Controller/LuckyController.php
132104 namespace App\Controller;
133105
134- use Symfony\Bundle\FrameworkBundle\Controller\Controller;
106+ + use Symfony\Bundle\FrameworkBundle\Controller\Controller;
135107
136- class LuckyController extends Controller
108+ - class LuckyController
109+ + class LuckyController extends Controller
137110 {
138111 // ...
139112 }
@@ -161,7 +134,7 @@ Generating URLs
161134The :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::generateUrl `
162135method is just a helper method that generates the URL for a given route::
163136
164- $url = $this->generateUrl('blog_show ', array('slug ' => 'slug-value' ));
137+ $url = $this->generateUrl('app_lucky_number ', array('max ' => 10 ));
165138
166139Redirecting
167140~~~~~~~~~~~
@@ -184,18 +157,16 @@ and ``redirect()`` methods::
184157 return $this->redirectToRoute('homepage', array(), 301);
185158
186159 // 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 ));
188161
189162 // redirect externally
190163 return $this->redirect('http://symfony.com/doc');
191164 }
192165
193- For more information, see the :doc: `Routing article </routing >`.
194-
195166.. caution ::
196167
197168 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
199170 to the `unvalidated redirects security vulnerability `_.
200171
201172.. index ::
0 commit comments