@@ -36,7 +36,7 @@ class::
3636 /**
3737 * @Route("/lucky/number/{max}", name="app_lucky_number")
3838 */
39- public function number($max)
39+ public function number(int $max): Response
4040 {
4141 $number = random_int(0, $max);
4242
@@ -134,7 +134,7 @@ and ``redirect()`` methods::
134134 use Symfony\Component\HttpFoundation\RedirectResponse;
135135
136136 // ...
137- public function index()
137+ public function index(): RedirectResponse
138138 {
139139 // redirects to the "homepage" route
140140 return $this->redirectToRoute('homepage');
@@ -196,12 +196,13 @@ If you need a service in a controller, type-hint an argument with its class
196196(or interface) name. Symfony will automatically pass you the service you need::
197197
198198 use Psr\Log\LoggerInterface;
199+ use Symfony\Component\HttpFoundation\Response;
199200 // ...
200201
201202 /**
202203 * @Route("/lucky/number/{max}")
203204 */
204- public function number($max, LoggerInterface $logger)
205+ public function number(int $max, LoggerInterface $logger): Response
205206 {
206207 $logger->info('We are logging!');
207208 // ...
@@ -322,10 +323,11 @@ Managing Errors and 404 Pages
322323When things are not found, you should return a 404 response. To do this, throw a
323324special type of exception::
324325
326+ use Symfony\Component\HttpFoundation\Response;
325327 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
326328
327329 // ...
328- public function index()
330+ public function index(): Response
329331 {
330332 // retrieve the object from database
331333 $product = ...;
@@ -370,8 +372,10 @@ object. To access it in your controller, add it as an argument and
370372**type-hint it with the Request class **::
371373
372374 use Symfony\Component\HttpFoundation\Request;
375+ use Symfony\Component\HttpFoundation\Response;
376+ // ...
373377
374- public function index(Request $request, $firstName, $lastName)
378+ public function index(Request $request, string $firstName, string $lastName): Response
375379 {
376380 $page = $request->query->get('page', 1);
377381
@@ -401,9 +405,11 @@ Session storage and other configuration can be controlled under the
401405To get the session, add an argument and type-hint it with
402406:class: `Symfony\\ Component\\ HttpFoundation\\ Session\\ SessionInterface `::
403407
408+ use Symfony\Component\HttpFoundation\Response;
404409 use Symfony\Component\HttpFoundation\Session\SessionInterface;
410+ // ...
405411
406- public function index(SessionInterface $session)
412+ public function index(SessionInterface $session): Response
407413 {
408414 // stores an attribute for reuse during a later user request
409415 $session->set('foo', 'bar');
@@ -413,6 +419,8 @@ To get the session, add an argument and type-hint it with
413419
414420 // uses a default value if the attribute doesn't exist
415421 $filters = $session->get('filters', []);
422+
423+ // ...
416424 }
417425
418426Stored attributes remain in the session for the remainder of that user's session.
@@ -435,8 +443,10 @@ from the session automatically as soon as you retrieve them. This feature makes
435443For example, imagine you're processing a :doc: `form </forms >` submission::
436444
437445 use Symfony\Component\HttpFoundation\Request;
446+ use Symfony\Component\HttpFoundation\Response;
447+ // ...
438448
439- public function update(Request $request)
449+ public function update(Request $request): Response
440450 {
441451 // ...
442452
@@ -515,8 +525,9 @@ pass the ``Request`` object to any controller argument that is type-hinted with
515525the ``Request `` class::
516526
517527 use Symfony\Component\HttpFoundation\Request;
528+ use Symfony\Component\HttpFoundation\Response;
518529
519- public function index(Request $request)
530+ public function index(Request $request): Response
520531 {
521532 $request->isXmlHttpRequest(); // is it an Ajax request?
522533
@@ -572,7 +583,7 @@ To get the value of any :ref:`configuration parameter <configuration-parameters>
572583from a controller, use the ``getParameter() `` helper method::
573584
574585 // ...
575- public function index()
586+ public function index(): Response
576587 {
577588 $contentsDir = $this->getParameter('kernel.project_dir').'/contents';
578589 // ...
@@ -584,8 +595,10 @@ Returning JSON Response
584595To return JSON from a controller, use the ``json() `` helper method. This returns a
585596``JsonResponse `` object that encodes the data automatically::
586597
598+ use Symfony\Component\HttpFoundation\Response;
587599 // ...
588- public function index()
600+
601+ public function index(): Response
589602 {
590603 // returns '{"username":"jane.doe"}' and sets the proper Content-Type header
591604 return $this->json(['username' => 'jane.doe']);
@@ -604,7 +617,10 @@ Streaming File Responses
604617You can use the :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ AbstractController::file `
605618helper to serve a file from inside a controller::
606619
607- public function download()
620+ use Symfony\Component\HttpFoundation\Response;
621+ // ...
622+
623+ public function download(): Response
608624 {
609625 // send the file contents and force the browser to download it
610626 return $this->file('/path/to/some_file.pdf');
@@ -614,8 +630,9 @@ The ``file()`` helper provides some arguments to configure its behavior::
614630
615631 use Symfony\Component\HttpFoundation\File\File;
616632 use Symfony\Component\HttpFoundation\ResponseHeaderBag;
633+ // ...
617634
618- public function download()
635+ public function download(): Response
619636 {
620637 // load the file from the filesystem
621638 $file = new File('/path/to/some_file.pdf');
0 commit comments