From ac2f7934fa8d246653b6528391f05dfe1807c616 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Wed, 19 Oct 2022 17:34:45 +0200 Subject: [PATCH] Document the PSR-4 route loader --- routing.rst | 17 ++++++++++++----- routing/custom_route_loader.rst | 31 ++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/routing.rst b/routing.rst index 72534ea3d00..b0ebdf341f8 100644 --- a/routing.rst +++ b/routing.rst @@ -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, +our kernel can act as a controller as well which is especially useful for small +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 ` like the following: diff --git a/routing/custom_route_loader.rst b/routing/custom_route_loader.rst index 7dd81a0f8b5..12538a78311 100644 --- a/routing/custom_route_loader.rst +++ b/routing/custom_route_loader.rst @@ -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/' @@ -51,9 +61,15 @@ Symfony provides several route loaders for the most common needs: + + + + + + @@ -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'); @@ -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. pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy