Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdb658ed

Browse files
committed
minor#20988 [FrameworkBundle] Enable controller service with#[Route] attribute (javiereguiluz)
This PR was merged into the 7.3 branch.Discussion----------[FrameworkBundle] Enable controller service with `#[Route]` attributeFixes#20839.This change required some updates in the article structure to better explain the three alternative solutions.Commits-------2c33c36 [FrameworkBundle] Enable controller service with #[Route] attribute
2 parentse1dd12e +2c33c36 commitdb658ed

File tree

2 files changed

+61
-25
lines changed

2 files changed

+61
-25
lines changed

‎controller/service.rst

Lines changed: 59 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,65 @@ and your controllers extend the `AbstractController`_ class, they *are* automati
77
registered as services. This means you can use dependency injection like any
88
other normal service.
99

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:
1569

1670
..configuration-block::
1771

@@ -58,26 +112,6 @@ in method parameters:
58112
calls:
59113
-[setContainer, ['@abstract_controller.locator']]
60114
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-
81115
Registering your controller as a service is the first step, but you also need to
82116
update your routing config to reference the service properly, so that Symfony
83117
knows to use it.

‎routing.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ your favorite.
1818
:ref:`Symfony recommends attributes<best-practice-controller-attributes>`
1919
because it's convenient to put the route and controller in the same place.
2020

21+
.. _routing-route-attributes:
22+
2123
Creating Routes as Attributes
2224
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2325

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp