@@ -232,13 +232,13 @@ Use the ``methods`` option to restrict the verbs each route should respond to:
232232 class BlogApiController extends AbstractController
233233 {
234234 #[Route('/api/posts/{id}', methods: ['GET', 'HEAD'])]
235- public function show(int $id)
235+ public function show(int $id): Response
236236 {
237237 // ... return a JSON response with the post
238238 }
239239
240240 #[Route('/api/posts/{id}', methods: ['PUT'])]
241- public function edit(int $id)
241+ public function edit(int $id): Response
242242 {
243243 // ... edit a post
244244 }
@@ -343,6 +343,7 @@ arbitrary matching logic:
343343 namespace App\Controller;
344344
345345 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
346+ use Symfony\Component\HttpFoundation\Response;
346347 use Symfony\Component\Routing\Annotation\Route;
347348
348349 class DefaultController extends AbstractController
@@ -354,7 +355,7 @@ arbitrary matching logic:
354355 )]
355356 // expressions can also include config parameters:
356357 // condition: "request.headers.get('User-Agent') matches '%app.allowed_browsers%'"
357- public function contact()
358+ public function contact(): Response
358359 {
359360 // ...
360361 }
@@ -517,14 +518,15 @@ defined as ``/blog/{slug}``:
517518 namespace App\Controller;
518519
519520 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
521+ use Symfony\Component\HttpFoundation\Response;
520522 use Symfony\Component\Routing\Annotation\Route;
521523
522524 class BlogController extends AbstractController
523525 {
524526 // ...
525527
526528 #[Route('/blog/{slug}', name: 'blog_show')]
527- public function show(string $slug)
529+ public function show(string $slug): Response
528530 {
529531 // $slug will equal the dynamic part of the URL
530532 // e.g. at /blog/yay-routing, then $slug='yay-routing'
@@ -623,18 +625,19 @@ the ``{page}`` parameter using the ``requirements`` option:
623625 namespace App\Controller;
624626
625627 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
628+ use Symfony\Component\HttpFoundation\Response;
626629 use Symfony\Component\Routing\Annotation\Route;
627630
628631 class BlogController extends AbstractController
629632 {
630633 #[Route('/blog/{page}', name: 'blog_list', requirements: ['page' => '\d+'])]
631- public function list(int $page)
634+ public function list(int $page): Response
632635 {
633636 // ...
634637 }
635638
636639 #[Route('/blog/{slug}', name: 'blog_show')]
637- public function show($slug)
640+ public function show($slug): Response
638641 {
639642 // ...
640643 }
@@ -750,12 +753,13 @@ concise, but it can decrease route readability when requirements are complex:
750753 namespace App\Controller;
751754
752755 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
756+ use Symfony\Component\HttpFoundation\Response;
753757 use Symfony\Component\Routing\Annotation\Route;
754758
755759 class BlogController extends AbstractController
756760 {
757761 #[Route('/blog/{page<\d+>}', name: 'blog_list')]
758- public function list(int $page)
762+ public function list(int $page): Response
759763 {
760764 // ...
761765 }
@@ -804,7 +808,7 @@ visit ``/blog/1``, it will match. But if they visit ``/blog``, it will **not**
804808match. As soon as you add a parameter to a route, it must have a value.
805809
806810You can make ``blog_list `` once again match when the user visits ``/blog `` by
807- adding a default value for the ``{page} `` parameter. When using annotations,
811+ adding a default value for the ``{page} `` parameter. When using annotations or attributes ,
808812default values are defined in the arguments of the controller action. In the
809813other configuration formats they are defined with the ``defaults `` option:
810814
@@ -836,12 +840,13 @@ other configuration formats they are defined with the ``defaults`` option:
836840 namespace App\Controller;
837841
838842 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
843+ use Symfony\Component\HttpFoundation\Response;
839844 use Symfony\Component\Routing\Annotation\Route;
840845
841846 class BlogController extends AbstractController
842847 {
843848 #[Route('/blog/{page}', name: 'blog_list', requirements: ['page' => '\d+'])]
844- public function list(int $page = 1)
849+ public function list(int $page = 1): Response
845850 {
846851 // ...
847852 }
@@ -940,12 +945,13 @@ parameter:
940945 namespace App\Controller;
941946
942947 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
948+ use Symfony\Component\HttpFoundation\Response;
943949 use Symfony\Component\Routing\Annotation\Route;
944950
945951 class BlogController extends AbstractController
946952 {
947953 #[Route('/blog/{page<\d+>?1}', name: 'blog_list')]
948- public function list(int $page)
954+ public function list(int $page): Response
949955 {
950956 // ...
951957 }
@@ -1194,7 +1200,7 @@ and in route imports. Symfony defines some special attributes with the same name
11941200 '_format' => 'html|xml',
11951201 ],
11961202 )]
1197- public function search()
1203+ public function search(): Response
11981204 {
11991205 }
12001206 }
@@ -1285,12 +1291,14 @@ the controllers of the routes:
12851291 // src/Controller/BlogController.php
12861292 namespace App\Controller;
12871293
1294+ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1295+ use Symfony\Component\HttpFoundation\Response;
12881296 use Symfony\Component\Routing\Annotation\Route;
12891297
1290- class BlogController
1298+ class BlogController extends AbstractController
12911299 {
12921300 #[Route('/blog/{page}', name: 'blog_index', defaults: ['page' => 1, 'title' => 'Hello world!'])]
1293- public function index(int $page, string $title)
1301+ public function index(int $page, string $title): Response
12941302 {
12951303 // ...
12961304 }
@@ -1375,13 +1383,15 @@ A possible solution is to change the parameter requirements to be more permissiv
13751383
13761384 // src/Controller/DefaultController.php
13771385 namespace App\Controller;
1378-
1386+
1387+ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1388+ use Symfony\Component\HttpFoundation\Response;
13791389 use Symfony\Component\Routing\Annotation\Route;
13801390
1381- class DefaultController
1391+ class DefaultController extends AbstractController
13821392 {
13831393 #[Route('/share/{token}', name: 'share', requirements: ['token' => '.+'])]
1384- public function share($token)
1394+ public function share($token): Response
13851395 {
13861396 // ...
13871397 }
@@ -1493,20 +1503,22 @@ when importing the routes.
14931503
14941504 // src/Controller/BlogController.php
14951505 namespace App\Controller;
1496-
1506+
1507+ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1508+ use Symfony\Component\HttpFoundation\Response;
14971509 use Symfony\Component\Routing\Annotation\Route;
14981510
14991511 #[Route('/blog', requirements: ['_locale' => 'en|es|fr'], name: 'blog_')]
1500- class BlogController
1512+ class BlogController extends AbstractController
15011513 {
15021514 #[Route('/{_locale}', name: 'index')]
1503- public function index()
1515+ public function index(): Response
15041516 {
15051517 // ...
15061518 }
15071519
15081520 #[Route('/{_locale}/posts/{slug}', name: 'show')]
1509- public function show(Post $post)
1521+ public function show(Post $post): Response
15101522 {
15111523 // ...
15121524 }
@@ -1837,18 +1849,19 @@ host name:
18371849 namespace App\Controller;
18381850
18391851 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1852+ use Symfony\Component\HttpFoundation\Response;
18401853 use Symfony\Component\Routing\Annotation\Route;
18411854
18421855 class MainController extends AbstractController
18431856 {
18441857 #[Route('/', name: 'mobile_homepage', host: 'm.example.com')]
1845- public function mobileHomepage()
1858+ public function mobileHomepage(): Response
18461859 {
18471860 // ...
18481861 }
18491862
18501863 #[Route('/', name: 'homepage')]
1851- public function homepage()
1864+ public function homepage(): Response
18521865 {
18531866 // ...
18541867 }
@@ -1946,6 +1959,7 @@ multi-tenant applications) and these parameters can be validated too with
19461959 namespace App\Controller;
19471960
19481961 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1962+ use Symfony\Component\HttpFoundation\Response;
19491963 use Symfony\Component\Routing\Annotation\Route;
19501964
19511965 class MainController extends AbstractController
@@ -1957,13 +1971,13 @@ multi-tenant applications) and these parameters can be validated too with
19571971 defaults: ['subdomain' => 'm'],
19581972 requirements: ['subdomain' => 'm|mobile'],
19591973 )]
1960- public function mobileHomepage()
1974+ public function mobileHomepage(): Response
19611975 {
19621976 // ...
19631977 }
19641978
19651979 #[Route('/', name: 'homepage')]
1966- public function homepage()
1980+ public function homepage(): Response
19671981 {
19681982 // ...
19691983 }
@@ -2101,6 +2115,7 @@ avoids the need for duplicating routes, which also reduces the potential bugs:
21012115 namespace App\Controller;
21022116
21032117 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
2118+ use Symfony\Component\HttpFoundation\Response;
21042119 use Symfony\Component\Routing\Annotation\Route;
21052120
21062121 class CompanyController extends AbstractController
@@ -2109,7 +2124,7 @@ avoids the need for duplicating routes, which also reduces the potential bugs:
21092124 'en' => '/about-us',
21102125 'nl' => '/over-ons'
21112126 ], name: 'about_us')]
2112- public function about()
2127+ public function about(): Response
21132128 {
21142129 // ...
21152130 }
@@ -2257,7 +2272,7 @@ locale.
22572272 $routes->import('../../src/Controller/', 'annotation')
22582273 ->host([
22592274 'en' => 'https://www.example.com',
2260- 'nl' => 'https://www.example.nl'
2275+ 'nl' => 'https://www.example.nl',
22612276 ])
22622277 ;
22632278 };
@@ -2711,12 +2726,13 @@ each route explicitly:
27112726 namespace App\Controller;
27122727
27132728 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
2729+ use Symfony\Component\HttpFoundation\Response;
27142730 use Symfony\Component\Routing\Annotation\Route;
27152731
27162732 class SecurityController extends AbstractController
27172733 {
27182734 #[Route('/login', name: 'login', schemes: ['https'])]
2719- public function login()
2735+ public function login(): Response
27202736 {
27212737 // ...
27222738 }
0 commit comments