@@ -232,13 +232,13 @@ Use the ``methods`` option to restrict the verbs each route should respond to:
232
232
class BlogApiController extends AbstractController
233
233
{
234
234
#[Route('/api/posts/{id}', methods: ['GET', 'HEAD'])]
235
- public function show(int $id)
235
+ public function show(int $id): Response
236
236
{
237
237
// ... return a JSON response with the post
238
238
}
239
239
240
240
#[Route('/api/posts/{id}', methods: ['PUT'])]
241
- public function edit(int $id)
241
+ public function edit(int $id): Response
242
242
{
243
243
// ... edit a post
244
244
}
@@ -343,6 +343,7 @@ arbitrary matching logic:
343
343
namespace App\Controller;
344
344
345
345
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
346
+ use Symfony\Component\HttpFoundation\Response;
346
347
use Symfony\Component\Routing\Annotation\Route;
347
348
348
349
class DefaultController extends AbstractController
@@ -354,7 +355,7 @@ arbitrary matching logic:
354
355
)]
355
356
// expressions can also include config parameters:
356
357
// condition: "request.headers.get('User-Agent') matches '%app.allowed_browsers%'"
357
- public function contact()
358
+ public function contact(): Response
358
359
{
359
360
// ...
360
361
}
@@ -517,14 +518,15 @@ defined as ``/blog/{slug}``:
517
518
namespace App\Controller;
518
519
519
520
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
521
+ use Symfony\Component\HttpFoundation\Response;
520
522
use Symfony\Component\Routing\Annotation\Route;
521
523
522
524
class BlogController extends AbstractController
523
525
{
524
526
// ...
525
527
526
528
#[Route('/blog/{slug}', name: 'blog_show')]
527
- public function show(string $slug)
529
+ public function show(string $slug): Response
528
530
{
529
531
// $slug will equal the dynamic part of the URL
530
532
// e.g. at /blog/yay-routing, then $slug='yay-routing'
@@ -623,18 +625,19 @@ the ``{page}`` parameter using the ``requirements`` option:
623
625
namespace App\Controller;
624
626
625
627
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
628
+ use Symfony\Component\HttpFoundation\Response;
626
629
use Symfony\Component\Routing\Annotation\Route;
627
630
628
631
class BlogController extends AbstractController
629
632
{
630
633
#[Route('/blog/{page}', name: 'blog_list', requirements: ['page' => '\d+'])]
631
- public function list(int $page)
634
+ public function list(int $page): Response
632
635
{
633
636
// ...
634
637
}
635
638
636
639
#[Route('/blog/{slug}', name: 'blog_show')]
637
- public function show($slug)
640
+ public function show($slug): Response
638
641
{
639
642
// ...
640
643
}
@@ -750,12 +753,13 @@ concise, but it can decrease route readability when requirements are complex:
750
753
namespace App\Controller;
751
754
752
755
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
756
+ use Symfony\Component\HttpFoundation\Response;
753
757
use Symfony\Component\Routing\Annotation\Route;
754
758
755
759
class BlogController extends AbstractController
756
760
{
757
761
#[Route('/blog/{page<\d+>}', name: 'blog_list')]
758
- public function list(int $page)
762
+ public function list(int $page): Response
759
763
{
760
764
// ...
761
765
}
@@ -804,7 +808,7 @@ visit ``/blog/1``, it will match. But if they visit ``/blog``, it will **not**
804
808
match. As soon as you add a parameter to a route, it must have a value.
805
809
806
810
You 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 ,
808
812
default values are defined in the arguments of the controller action. In the
809
813
other configuration formats they are defined with the ``defaults `` option:
810
814
@@ -836,12 +840,13 @@ other configuration formats they are defined with the ``defaults`` option:
836
840
namespace App\Controller;
837
841
838
842
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
843
+ use Symfony\Component\HttpFoundation\Response;
839
844
use Symfony\Component\Routing\Annotation\Route;
840
845
841
846
class BlogController extends AbstractController
842
847
{
843
848
#[Route('/blog/{page}', name: 'blog_list', requirements: ['page' => '\d+'])]
844
- public function list(int $page = 1)
849
+ public function list(int $page = 1): Response
845
850
{
846
851
// ...
847
852
}
@@ -940,12 +945,13 @@ parameter:
940
945
namespace App\Controller;
941
946
942
947
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
948
+ use Symfony\Component\HttpFoundation\Response;
943
949
use Symfony\Component\Routing\Annotation\Route;
944
950
945
951
class BlogController extends AbstractController
946
952
{
947
953
#[Route('/blog/{page<\d+>?1}', name: 'blog_list')]
948
- public function list(int $page)
954
+ public function list(int $page): Response
949
955
{
950
956
// ...
951
957
}
@@ -1194,7 +1200,7 @@ and in route imports. Symfony defines some special attributes with the same name
1194
1200
'_format' => 'html|xml',
1195
1201
],
1196
1202
)]
1197
- public function search()
1203
+ public function search(): Response
1198
1204
{
1199
1205
}
1200
1206
}
@@ -1285,12 +1291,14 @@ the controllers of the routes:
1285
1291
// src/Controller/BlogController.php
1286
1292
namespace App\Controller;
1287
1293
1294
+ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1295
+ use Symfony\Component\HttpFoundation\Response;
1288
1296
use Symfony\Component\Routing\Annotation\Route;
1289
1297
1290
- class BlogController
1298
+ class BlogController extends AbstractController
1291
1299
{
1292
1300
#[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
1294
1302
{
1295
1303
// ...
1296
1304
}
@@ -1375,13 +1383,15 @@ A possible solution is to change the parameter requirements to be more permissiv
1375
1383
1376
1384
// src/Controller/DefaultController.php
1377
1385
namespace App\Controller;
1378
-
1386
+
1387
+ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1388
+ use Symfony\Component\HttpFoundation\Response;
1379
1389
use Symfony\Component\Routing\Annotation\Route;
1380
1390
1381
- class DefaultController
1391
+ class DefaultController extends AbstractController
1382
1392
{
1383
1393
#[Route('/share/{token}', name: 'share', requirements: ['token' => '.+'])]
1384
- public function share($token)
1394
+ public function share($token): Response
1385
1395
{
1386
1396
// ...
1387
1397
}
@@ -1493,20 +1503,22 @@ when importing the routes.
1493
1503
1494
1504
// src/Controller/BlogController.php
1495
1505
namespace App\Controller;
1496
-
1506
+
1507
+ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1508
+ use Symfony\Component\HttpFoundation\Response;
1497
1509
use Symfony\Component\Routing\Annotation\Route;
1498
1510
1499
1511
#[Route('/blog', requirements: ['_locale' => 'en|es|fr'], name: 'blog_')]
1500
- class BlogController
1512
+ class BlogController extends AbstractController
1501
1513
{
1502
1514
#[Route('/{_locale}', name: 'index')]
1503
- public function index()
1515
+ public function index(): Response
1504
1516
{
1505
1517
// ...
1506
1518
}
1507
1519
1508
1520
#[Route('/{_locale}/posts/{slug}', name: 'show')]
1509
- public function show(Post $post)
1521
+ public function show(Post $post): Response
1510
1522
{
1511
1523
// ...
1512
1524
}
@@ -1837,18 +1849,19 @@ host name:
1837
1849
namespace App\Controller;
1838
1850
1839
1851
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1852
+ use Symfony\Component\HttpFoundation\Response;
1840
1853
use Symfony\Component\Routing\Annotation\Route;
1841
1854
1842
1855
class MainController extends AbstractController
1843
1856
{
1844
1857
#[Route('/', name: 'mobile_homepage', host: 'm.example.com')]
1845
- public function mobileHomepage()
1858
+ public function mobileHomepage(): Response
1846
1859
{
1847
1860
// ...
1848
1861
}
1849
1862
1850
1863
#[Route('/', name: 'homepage')]
1851
- public function homepage()
1864
+ public function homepage(): Response
1852
1865
{
1853
1866
// ...
1854
1867
}
@@ -1946,6 +1959,7 @@ multi-tenant applications) and these parameters can be validated too with
1946
1959
namespace App\Controller;
1947
1960
1948
1961
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1962
+ use Symfony\Component\HttpFoundation\Response;
1949
1963
use Symfony\Component\Routing\Annotation\Route;
1950
1964
1951
1965
class MainController extends AbstractController
@@ -1957,13 +1971,13 @@ multi-tenant applications) and these parameters can be validated too with
1957
1971
defaults: ['subdomain' => 'm'],
1958
1972
requirements: ['subdomain' => 'm|mobile'],
1959
1973
)]
1960
- public function mobileHomepage()
1974
+ public function mobileHomepage(): Response
1961
1975
{
1962
1976
// ...
1963
1977
}
1964
1978
1965
1979
#[Route('/', name: 'homepage')]
1966
- public function homepage()
1980
+ public function homepage(): Response
1967
1981
{
1968
1982
// ...
1969
1983
}
@@ -2101,6 +2115,7 @@ avoids the need for duplicating routes, which also reduces the potential bugs:
2101
2115
namespace App\Controller;
2102
2116
2103
2117
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
2118
+ use Symfony\Component\HttpFoundation\Response;
2104
2119
use Symfony\Component\Routing\Annotation\Route;
2105
2120
2106
2121
class CompanyController extends AbstractController
@@ -2109,7 +2124,7 @@ avoids the need for duplicating routes, which also reduces the potential bugs:
2109
2124
'en' => '/about-us',
2110
2125
'nl' => '/over-ons'
2111
2126
], name: 'about_us')]
2112
- public function about()
2127
+ public function about(): Response
2113
2128
{
2114
2129
// ...
2115
2130
}
@@ -2257,7 +2272,7 @@ locale.
2257
2272
$routes->import('../../src/Controller/', 'annotation')
2258
2273
->host([
2259
2274
'en' => 'https://www.example.com',
2260
- 'nl' => 'https://www.example.nl'
2275
+ 'nl' => 'https://www.example.nl',
2261
2276
])
2262
2277
;
2263
2278
};
@@ -2711,12 +2726,13 @@ each route explicitly:
2711
2726
namespace App\Controller;
2712
2727
2713
2728
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
2729
+ use Symfony\Component\HttpFoundation\Response;
2714
2730
use Symfony\Component\Routing\Annotation\Route;
2715
2731
2716
2732
class SecurityController extends AbstractController
2717
2733
{
2718
2734
#[Route('/login', name: 'login', schemes: ['https'])]
2719
- public function login()
2735
+ public function login(): Response
2720
2736
{
2721
2737
// ...
2722
2738
}
0 commit comments