@@ -7,11 +7,65 @@ and your controllers extend the `AbstractController`_ class, they *are* automati
7
7
registered as services. This means you can use dependency injection like any
8
8
other normal service.
9
9
10
- If your controllers don't extend the `AbstractController `_ class, you must
11
- explicitly mark your controller services as ``public ``. Alternatively, you can
12
- apply the ``controller.service_arguments `` tag to your controller services. This
13
- will make the tagged services ``public `` and will allow you to inject services
14
- in method parameters:
10
+ If you prefer to not extend the ``AbstractController `` class, you can register
11
+ your controllers as services in several ways:
12
+
13
+ #. Using the ``#[Route] `` attribute;
14
+ #. Using the ``#[AsController] `` attribute;
15
+ #. Using the ``controller.service_arguments `` service tag.
16
+
17
+ Using the ``#[Route] `` Attribute
18
+ --------------------------------
19
+
20
+ When using:ref: `the #[Route] attribute <routing-route-attributes >` to define
21
+ routes on any PHP class, Symfony treats that class as a controller. It registers
22
+ it as a public, non-lazy service and enables service argument injection in all
23
+ its methods.
24
+
25
+ This is the simplest and recommended way to register controllers as services
26
+ when not extending the base controller class.
27
+
28
+ ..versionadded ::7.3
29
+
30
+ The feature to register controllers as services when using the ``#[Route] ``
31
+ attribute was introduced in Symfony 7.3.
32
+
33
+ Using the ``#[AsController] `` Attribute
34
+ ---------------------------------------
35
+
36
+ If you prefer, you can use the ``#[AsController] `` PHP attribute to automatically
37
+ apply the ``controller.service_arguments `` tag to your controller services::
38
+
39
+ // src/Controller/HelloController.php
40
+ namespace App\Controller;
41
+
42
+ use Symfony\Component\HttpFoundation\Response;
43
+ use Symfony\Component\HttpKernel\Attribute\AsController;
44
+ use Symfony\Component\Routing\Attribute\Route;
45
+
46
+ #[AsController]
47
+ class HelloController
48
+ {
49
+ #[Route('/hello', name: 'hello', methods: ['GET'])]
50
+ public function index(): Response
51
+ {
52
+ // ...
53
+ }
54
+ }
55
+
56
+ ..tip ::
57
+
58
+ When using the ``#[Route] `` attribute, Symfony already registers the controller
59
+ class as a service, so using the ``#[AsController] `` attribute is redundant.
60
+
61
+ Using the ``controller.service_arguments `` Service Tag
62
+ ------------------------------------------------------
63
+
64
+ If your controllers don't extend the `AbstractController `_ class and you don't
65
+ use the ``#[AsController] `` or ``#[Route] `` attributes, you must register the
66
+ controllers as public services manually and apply the ``controller.service_arguments ``
67
+ :doc: `service tag </service_container/tags >` to enable service injection in
68
+ controller actions:
15
69
16
70
..configuration-block ::
17
71
@@ -58,26 +112,6 @@ in method parameters:
58
112
calls :
59
113
-[setContainer, ['@abstract_controller.locator']]
60
114
61
- If you prefer, you can use the ``#[AsController] `` PHP attribute to automatically
62
- apply the ``controller.service_arguments `` tag to your controller services::
63
-
64
- // src/Controller/HelloController.php
65
- namespace App\Controller;
66
-
67
- use Symfony\Component\HttpFoundation\Response;
68
- use Symfony\Component\HttpKernel\Attribute\AsController;
69
- use Symfony\Component\Routing\Attribute\Route;
70
-
71
- #[AsController]
72
- class HelloController
73
- {
74
- #[Route('/hello', name: 'hello', methods: ['GET'])]
75
- public function index(): Response
76
- {
77
- // ...
78
- }
79
- }
80
-
81
115
Registering your controller as a service is the first step, but you also need to
82
116
update your routing config to reference the service properly, so that Symfony
83
117
knows to use it.