Skip to content

Commit 4d1acb5

Browse files
committed
minor #17373 Document the PSR-4 route loader (derrabus)
This PR was merged into the 6.2 branch. Discussion ---------- Document the PSR-4 route loader This PR documents symfony/symfony#47916 Although the PR does not deprecate `AnnotationDirectoryLoader` and `AnnotationFileLoader`, I chose to recommend only the new `Psr4DirectoryLoader` and `AnnotationDirectoryLoader` in `routing.rst`. In `routing/custom_route_loader.rst`, all four loaders are listed for completeness. Using a class name as resource instead of a file path has always worked, but wasn't documented for some reason. I have added examples for that as well. Commits ------- ac2f793 Document the PSR-4 route loader
2 parents cc07180 + ac2f793 commit 4d1acb5

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

routing.rst

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,22 @@ Otherwise, create the following file manually:
3737
# config/routes/attributes.yaml
3838
controllers:
3939
resource: ../../src/Controller/
40-
type: attribute
40+
type: attribute@App\Controller
4141
4242
kernel:
43-
resource: ../../src/Kernel.php
43+
resource: App\Kernel
4444
type: attribute
4545
46-
This configuration tells Symfony to look for routes defined as
47-
attributes in any PHP class stored in the ``src/Controller/``
48-
directory.
46+
This configuration tells Symfony to look for routes defined as attributes on
47+
classes declared in the ``App\Controller`` namespace which are stored in the
48+
``src/Controller/`` directory which follows the PSR-4 standard. In addition,
49+
our kernel can act as a controller as well which is especially useful for small
50+
applications that use Symfony as a microframework.
51+
52+
.. versionadded:: 6.2
53+
54+
The possibility to suffix the ``attribute`` resource type with a PSR-4
55+
namespace root was introduced in Symfony 6.2.
4956

5057
Suppose you want to define a route for the ``/blog`` URL in your application. To
5158
do so, create a :doc:`controller class </controller>` like the following:

routing/custom_route_loader.rst

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,21 @@ Symfony provides several route loaders for the most common needs:
2424
# loads routes from the given routing file stored in some bundle
2525
resource: '@AcmeBundle/Resources/config/routing.yaml'
2626
27+
app_psr4:
28+
# loads routes from the PHP attributes of the controllers found in the given PSR-4 namespace root
29+
resource: '../src/Controller/'
30+
type: attribute@App\Controller
31+
2732
app_attributes:
2833
# loads routes from the PHP attributes of the controllers found in that directory
2934
resource: '../src/Controller/'
3035
type: attribute
3136
37+
app_class_attributes:
38+
# loads routes from the PHP attributes of the given class
39+
resource: App\Controller\MyController
40+
type: attribute
41+
3242
app_directory:
3343
# loads routes from the YAML, XML or PHP files found in that directory
3444
resource: '../legacy/routing/'
@@ -51,9 +61,15 @@ Symfony provides several route loaders for the most common needs:
5161
<!-- loads routes from the given routing file stored in some bundle -->
5262
<import resource="@AcmeBundle/Resources/config/routing.yaml"/>
5363
64+
<!-- loads routes from the PHP attributes of the controllers found in the given PSR-4 namespace root -->
65+
<import resource="../src/Controller/" type="attribute@App\Controller"/>
66+
5467
<!-- loads routes from the PHP attributes of the controllers found in that directory -->
5568
<import resource="../src/Controller/" type="attribute"/>
5669
70+
<!-- loads routes from the PHP attributes of the given class -->
71+
<import resource="App\Controller\MyController" type="attribute"/>
72+
5773
<!-- loads routes from the YAML or XML files found in that directory -->
5874
<import resource="../legacy/routing/" type="directory"/>
5975
@@ -70,9 +86,17 @@ Symfony provides several route loaders for the most common needs:
7086
// loads routes from the given routing file stored in some bundle
7187
$routes->import('@AcmeBundle/Resources/config/routing.yaml');
7288
73-
// loads routes from the PHP attributes (#[Route(...)]) of the controllers found in that directory
89+
// loads routes from the PHP attributes (#[Route(...)])
90+
// of the controllers found in the given PSR-4 namespace root
91+
$routes->import('../src/Controller/', 'attribute@App\Controller');
92+
93+
// loads routes from the PHP attributes (#[Route(...)])
94+
// of the controllers found in that directory
7495
$routes->import('../src/Controller/', 'attribute');
7596
97+
// loads routes from the PHP attributes (#[Route(...)]) of the given class
98+
$routes->import('App\Controller\MyController', 'attribute');
99+
76100
// loads routes from the YAML or XML files found in that directory
77101
$routes->import('../legacy/routing/', 'directory');
78102
@@ -85,6 +109,11 @@ Symfony provides several route loaders for the most common needs:
85109
The ``attribute`` value of the second argument of ``import()`` was introduced
86110
in Symfony 6.1.
87111

112+
.. versionadded:: 6.2
113+
114+
The possibility to suffix the ``attribute`` resource type with a PSR-4
115+
namespace root was introduced in Symfony 6.2.
116+
88117
.. note::
89118

90119
When importing resources, the key (e.g. ``app_file``) is the name of the collection.

0 commit comments

Comments
 (0)