Skip to content

[Routing] Document the PSR-4 route loader #17373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions routing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,22 @@ Otherwise, create the following file manually:
# config/routes/attributes.yaml
controllers:
resource: ../../src/Controller/
type: attribute
type: attribute@App\Controller

kernel:
resource: ../../src/Kernel.php
resource: App\Kernel
type: attribute

This configuration tells Symfony to look for routes defined as
attributes in any PHP class stored in the ``src/Controller/``
directory.
This configuration tells Symfony to look for routes defined as attributes on
classes declared in the ``App\Controller`` namespace which are stored in the
``src/Controller/`` directory which follows the PSR-4 standard. In addition,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing colon directory, which follows?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, it feels very German to set that comma. Let's see what the docs team says.

our kernel can act as a controller as well which is especially useful for small
Copy link
Contributor

@maxbeckers maxbeckers Oct 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here as well act as a controller as well, which?

applications that use Symfony as a microframework.

.. versionadded:: 6.2

The possibility to suffix the ``attribute`` resource type with a PSR-4
namespace root was introduced in Symfony 6.2.

Suppose you want to define a route for the ``/blog`` URL in your application. To
do so, create a :doc:`controller class </controller>` like the following:
Expand Down
31 changes: 30 additions & 1 deletion routing/custom_route_loader.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,21 @@ Symfony provides several route loaders for the most common needs:
# loads routes from the given routing file stored in some bundle
resource: '@AcmeBundle/Resources/config/routing.yaml'

app_psr4:
# loads routes from the PHP attributes of the controllers found in the given PSR-4 namespace root
resource: '../src/Controller/'
type: attribute@App\Controller

app_attributes:
# loads routes from the PHP attributes of the controllers found in that directory
resource: '../src/Controller/'
type: attribute

app_class_attributes:
# loads routes from the PHP attributes of the given class
resource: App\Controller\MyController
type: attribute

app_directory:
# loads routes from the YAML, XML or PHP files found in that directory
resource: '../legacy/routing/'
Expand All @@ -51,9 +61,15 @@ Symfony provides several route loaders for the most common needs:
<!-- loads routes from the given routing file stored in some bundle -->
<import resource="@AcmeBundle/Resources/config/routing.yaml"/>

<!-- loads routes from the PHP attributes of the controllers found in the given PSR-4 namespace root -->
<import resource="../src/Controller/" type="attribute@App\Controller"/>

<!-- loads routes from the PHP attributes of the controllers found in that directory -->
<import resource="../src/Controller/" type="attribute"/>

<!-- loads routes from the PHP attributes of the given class -->
<import resource="App\Controller\MyController" type="attribute"/>

<!-- loads routes from the YAML or XML files found in that directory -->
<import resource="../legacy/routing/" type="directory"/>

Expand All @@ -70,9 +86,17 @@ Symfony provides several route loaders for the most common needs:
// loads routes from the given routing file stored in some bundle
$routes->import('@AcmeBundle/Resources/config/routing.yaml');

// loads routes from the PHP attributes (#[Route(...)]) of the controllers found in that directory
// loads routes from the PHP attributes (#[Route(...)])
// of the controllers found in the given PSR-4 namespace root
$routes->import('../src/Controller/', 'attribute@App\Controller');

// loads routes from the PHP attributes (#[Route(...)])
// of the controllers found in that directory
$routes->import('../src/Controller/', 'attribute');

// loads routes from the PHP attributes (#[Route(...)]) of the given class
$routes->import('App\Controller\MyController', 'attribute');

// loads routes from the YAML or XML files found in that directory
$routes->import('../legacy/routing/', 'directory');

Expand All @@ -85,6 +109,11 @@ Symfony provides several route loaders for the most common needs:
The ``attribute`` value of the second argument of ``import()`` was introduced
in Symfony 6.1.

.. versionadded:: 6.2

The possibility to suffix the ``attribute`` resource type with a PSR-4
namespace root was introduced in Symfony 6.2.

.. note::

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