From d245b451b2ba323350d61f5a719a76d340b6f78f Mon Sep 17 00:00:00 2001 From: Jules Pietri Date: Mon, 4 Mar 2019 21:46:37 +0100 Subject: [PATCH 1/2] Fixed route examples --- components/routing.rst | 7 ++- controller/service.rst | 11 ++--- routing.rst | 12 ++--- routing/conditions.rst | 5 +- routing/extra_information.rst | 5 +- routing/hostname_pattern.rst | 58 ++++++++++++------------ routing/optional_placeholders.rst | 11 ++--- routing/redirect_in_config.rst | 27 +++++------ routing/requirements.rst | 28 +++++++----- routing/scheme.rst | 4 +- routing/service_container_parameters.rst | 9 ++-- routing/slash_in_parameter.rst | 3 +- security/form_login.rst | 9 ++-- security/json_login_setup.rst | 9 ++-- templating.rst | 21 +++++---- templating/render_without_controller.rst | 18 +++++--- translation/locale.rst | 2 +- 17 files changed, 118 insertions(+), 121 deletions(-) diff --git a/components/routing.rst b/components/routing.rst index bad49425614..0cb92a7ece5 100644 --- a/components/routing.rst +++ b/components/routing.rst @@ -422,8 +422,7 @@ routes with UTF-8 characters: xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - - App\Controller\DefaultController::category + @@ -498,8 +497,8 @@ You can also include UTF-8 strings as routing requirements: xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - - App\Controller\DefaultController::default + 한국어 diff --git a/controller/service.rst b/controller/service.rst index 8fbb6db9a2f..082feb6cfc7 100644 --- a/controller/service.rst +++ b/controller/service.rst @@ -32,7 +32,7 @@ a service like: ``App\Controller\HelloController::index``: class HelloController { /** - * @Route("/hello", name="hello") + * @Route("/hello", name="hello", methods={"GET"}) */ public function index() { @@ -45,7 +45,8 @@ a service like: ``App\Controller\HelloController::index``: # config/routes.yaml hello: path: /hello - defaults: { _controller: App\Controller\HelloController::index } + controller: App\Controller\HelloController::index + methods: GET .. code-block:: xml @@ -56,9 +57,7 @@ a service like: ``App\Controller\HelloController::index``: xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - - App\Controller\HelloController::index - + @@ -67,7 +66,7 @@ a service like: ``App\Controller\HelloController::index``: // config/routes.php $collection->add('hello', new Route('/hello', [ '_controller' => 'App\Controller\HelloController::index', - ])); + ], [], [], '', [], ['GET'])); .. _controller-service-invoke: diff --git a/routing.rst b/routing.rst index d5e84a2da6c..f77081f9bec 100644 --- a/routing.rst +++ b/routing.rst @@ -90,11 +90,11 @@ Now you can configure the routes: xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - + - + @@ -102,9 +102,9 @@ Now you can configure the routes: .. code-block:: php // config/routes.php + use App\Controller\BlogController; use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\Route; - use App\Controller\BlogController; $routes = new RouteCollection(); $routes->add('blog_list', new Route('/blog', [ @@ -407,8 +407,7 @@ concise, but it can decrease route readability when requirements are complex: xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - + @@ -572,8 +571,7 @@ placeholder: xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - + diff --git a/routing/conditions.rst b/routing/conditions.rst index 76962a7d464..c98b62767ab 100644 --- a/routing/conditions.rst +++ b/routing/conditions.rst @@ -55,8 +55,9 @@ define arbitrary matching logic, use the ``conditions`` routing option: xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - - App\Controller\DefaultController::contact + context.getMethod() in ['GET', 'HEAD'] and request.headers.get('User-Agent') matches '/firefox/i' diff --git a/routing/extra_information.rst b/routing/extra_information.rst index 295d7926ae8..5fc3d144b38 100644 --- a/routing/extra_information.rst +++ b/routing/extra_information.rst @@ -30,8 +30,9 @@ to your controller, and as attributes of the ``Request`` object: xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - - App\Controller\BlogController::index + 1 Hello world! diff --git a/routing/hostname_pattern.rst b/routing/hostname_pattern.rst index d3ef58522a7..ec9d51d5590 100644 --- a/routing/hostname_pattern.rst +++ b/routing/hostname_pattern.rst @@ -56,13 +56,12 @@ You can also match on the HTTP *host* of the incoming request. xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - - App\Controller\MainController::mobileHomepage - + - - App\Controller\MainController::homepage - + .. code-block:: php @@ -104,9 +103,9 @@ you can use placeholders in your hostname: class MainController extends AbstractController { /** - * @Route("/", name="projects_homepage", host="{project_name}.example.com") + * @Route("/", name="projects_homepage", host="{project}.example.com") */ - public function projectsHomepage() + public function projectsHomepage(string $project) { // ... } @@ -125,7 +124,7 @@ you can use placeholders in your hostname: # config/routes.yaml projects_homepage: path: / - host: "{project_name}.example.com" + host: "{project}.example.com" controller: App\Controller\MainController::projectsHomepage homepage: @@ -141,13 +140,12 @@ you can use placeholders in your hostname: xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - - App\Controller\MainController::projectsHomepage - + - - App\Controller\MainController::homepage - + .. code-block:: php @@ -159,7 +157,7 @@ you can use placeholders in your hostname: $routes = new RouteCollection(); $routes->add('project_homepage', new Route('/', [ '_controller' => 'App\Controller\MainController::projectsHomepage', - ], [], [], '{project_name}.example.com')); + ], [], [], '{project}.example.com')); $routes->add('homepage', new Route('/', [ '_controller' => 'App\Controller\MainController::homepage', @@ -231,15 +229,15 @@ instance, if you want to match both ``m.example.com`` and xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - - App\Controller\MainController::mobileHomepage + m m|mobile - - App\Controller\MainController::homepage - + .. code-block:: php @@ -327,15 +325,15 @@ instance, if you want to match both ``m.example.com`` and xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - - App\Controller\MainController::mobileHomepage + %domain% %domain% - - App\Controller\MainController::homepage - + .. code-block:: php @@ -376,7 +374,7 @@ You can also set the host option on imported routes: .. code-block:: php-annotations // src/Controller/MainController.php - namespace App\Controller; + namespace Acme\HelloBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Routing\Annotation\Route; @@ -393,7 +391,7 @@ You can also set the host option on imported routes: # config/routes.yaml app_hello: - resource: '@ThirdPartyBundle/Resources/config/routing.yaml' + resource: '@AcmeHelloBundle/Resources/config/routing.yaml' host: "hello.example.com" .. code-block:: xml @@ -405,13 +403,13 @@ You can also set the host option on imported routes: xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - + .. code-block:: php // config/routes.php - $routes = $loader->import("@ThirdPartyBundle/Resources/config/routing.php"); + $routes = $loader->import("@AcmeHelloBundle/Resources/config/routing.php"); $routes->setHost('hello.example.com'); return $routes; diff --git a/routing/optional_placeholders.rst b/routing/optional_placeholders.rst index 0fda2982f8f..4475bfcc179 100644 --- a/routing/optional_placeholders.rst +++ b/routing/optional_placeholders.rst @@ -43,9 +43,7 @@ the available blog posts for this imaginary blog application: xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - - App\Controller\BlogController::index - + .. code-block:: php @@ -98,9 +96,7 @@ entries? Update the route to have a new ``{page}`` placeholder: xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - - App\Controller\BlogController::index - + .. code-block:: php @@ -159,8 +155,7 @@ This is done by including it in the ``defaults`` collection: xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - - App\Controller\BlogController::index + 1 diff --git a/routing/redirect_in_config.rst b/routing/redirect_in_config.rst index 974fb88b5b1..beb9e45c841 100644 --- a/routing/redirect_in_config.rst +++ b/routing/redirect_in_config.rst @@ -28,7 +28,7 @@ action to redirect to this new url: # load some routes - one should ultimately have the path "/app" controllers: - resource: ../src/Controller/ + resource: '../src/Controller/' type: annotation prefix: /app @@ -50,14 +50,12 @@ action to redirect to this new url: http://symfony.com/schema/routing/routing-1.0.xsd"> - + - - Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction + /app true @@ -129,8 +127,9 @@ action: - - Symfony\Bundle\FrameworkBundle\Controller\RedirectController::redirectAction + sonata_admin_dashboard true @@ -218,17 +217,19 @@ permanent redirects use ``308`` code instead of ``301``: http://symfony.com/schema/routing/routing-1.0.xsd"> - + - Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction true true - + - Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction false true diff --git a/routing/requirements.rst b/routing/requirements.rst index 1c6df7c77ba..4e4e05e486c 100644 --- a/routing/requirements.rst +++ b/routing/requirements.rst @@ -47,8 +47,9 @@ a routing ``{wildcard}`` to only match some regular expression: xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - - App\Controller\BlogController::list + \d+ @@ -125,8 +126,9 @@ URL: xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - - App\Controller\MainController::homepage + en en|fr @@ -221,12 +223,12 @@ accomplished with the following route configuration: api_post_show: path: /api/posts/{id} controller: App\Controller\BlogApiController::show - methods: [GET, HEAD] + methods: GET|HEAD api_post_edit: path: /api/posts/{id} controller: App\Controller\BlogApiController::edit - methods: [PUT] + methods: PUT .. code-block:: xml @@ -237,13 +239,15 @@ accomplished with the following route configuration: xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - - App\Controller\BlogApiController::show - + - - App\Controller\BlogApiController::edit - + .. code-block:: php diff --git a/routing/scheme.rst b/routing/scheme.rst index d2519e895a3..ef662191eba 100644 --- a/routing/scheme.rst +++ b/routing/scheme.rst @@ -46,9 +46,7 @@ the URI scheme via schemes: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - - App\Controller\MainController::secure - + .. code-block:: php diff --git a/routing/service_container_parameters.rst b/routing/service_container_parameters.rst index 35e248232e7..ebe325ae21e 100644 --- a/routing/service_container_parameters.rst +++ b/routing/service_container_parameters.rst @@ -34,8 +34,7 @@ inside your routing configuration: xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - - App\Controller\MainController::contact + %app.locales% @@ -106,9 +105,9 @@ path): xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - - App\Controller\MainController::contact - + .. code-block:: php diff --git a/routing/slash_in_parameter.rst b/routing/slash_in_parameter.rst index c0a1cc99bec..f1b767942f3 100644 --- a/routing/slash_in_parameter.rst +++ b/routing/slash_in_parameter.rst @@ -57,8 +57,7 @@ a more permissive regular expression for it: xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - - App\Controller\DefaultController::share + .+ diff --git a/security/form_login.rst b/security/form_login.rst index f825d1f8cc7..43fb8567a6c 100644 --- a/security/form_login.rst +++ b/security/form_login.rst @@ -100,7 +100,7 @@ configuration (``login``): class SecurityController extends AbstractController { /** - * @Route("/login", name="login") + * @Route("/login", name="login", methods={"GET", "POST"}) */ public function login() { @@ -113,6 +113,7 @@ configuration (``login``): login: path: /login controller: App\Controller\SecurityController::login + methods: GET|POST .. code-block:: xml @@ -123,9 +124,7 @@ configuration (``login``): xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - - App\Controller\SecurityController::login - + .. code-block:: php @@ -138,7 +137,7 @@ configuration (``login``): $routes = new RouteCollection(); $routes->add('login', new Route('/login', [ '_controller' => [SecurityController::class, 'login'], - ])); + ], [], [], '', ['GET', 'POST'])); return $routes; diff --git a/security/json_login_setup.rst b/security/json_login_setup.rst index ed019c7b13f..2ef3236ddec 100644 --- a/security/json_login_setup.rst +++ b/security/json_login_setup.rst @@ -74,7 +74,7 @@ The next step is to configure a route in your app matching this path: class SecurityController extends AbstractController { /** - * @Route("/login", name="login") + * @Route("/login", name="login", methods={"POST"}) */ public function login(Request $request) { @@ -93,6 +93,7 @@ The next step is to configure a route in your app matching this path: login: path: /login controller: App\Controller\SecurityController::login + methods: POST .. code-block:: xml @@ -103,9 +104,7 @@ The next step is to configure a route in your app matching this path: xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - - App\Controller\SecurityController::login - + .. code-block:: php @@ -117,7 +116,7 @@ The next step is to configure a route in your app matching this path: $routes = new RouteCollection(); $routes->add('login', new Route('/login', [ '_controller' => 'App\Controller\SecurityController::login', - ])); + ], [], [], '', [], ['POST'])); return $routes; diff --git a/templating.rst b/templating.rst index 6ccb6447716..326bca3e545 100644 --- a/templating.rst +++ b/templating.rst @@ -499,7 +499,7 @@ configuration: class WelcomeController extends AbstractController { /** - * @Route("/", name="welcome") + * @Route("/", name="welcome", methods={"GET"}) */ public function index() { @@ -513,6 +513,7 @@ configuration: welcome: path: / controller: App\Controller\WelcomeController::index + methods: GET .. code-block:: xml @@ -523,9 +524,7 @@ configuration: xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - - App\Controller\WelcomeController::index - + .. code-block:: php @@ -537,7 +536,7 @@ configuration: $routes = new RouteCollection(); $routes->add('welcome', new Route('/', [ '_controller' => 'App\Controller\WelcomeController::index', - ])); + ], [], [], '', [], ['GET'])); return $routes; @@ -562,7 +561,7 @@ route: class ArticleController extends AbstractController { /** - * @Route("/article/{slug}", name="article_show") + * @Route("/article/{slug}", name="article_show", methods={"GET"}) */ public function show($slug) { @@ -576,6 +575,7 @@ route: article_show: path: /article/{slug} controller: App\Controller\ArticleController::show + methods: 'GET' .. code-block:: xml @@ -586,9 +586,10 @@ route: xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - - App\Controller\ArticleController::show - + .. code-block:: php @@ -600,7 +601,7 @@ route: $routes = new RouteCollection(); $routes->add('article_show', new Route('/article/{slug}', [ '_controller' => 'App\Controller\ArticleController::show', - ])); + ], [], [], '', [], ['GET'])); return $routes; diff --git a/templating/render_without_controller.rst b/templating/render_without_controller.rst index 820611a82e4..fe5ed082593 100644 --- a/templating/render_without_controller.rst +++ b/templating/render_without_controller.rst @@ -25,6 +25,7 @@ can do this without creating a controller: controller: Symfony\Bundle\FrameworkBundle\Controller\TemplateController defaults: template: static/privacy.html.twig + methods: GET .. code-block:: xml @@ -34,8 +35,10 @@ can do this without creating a controller: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - - Symfony\Bundle\FrameworkBundle\Controller\TemplateController + static/privacy.html.twig @@ -50,7 +53,7 @@ can do this without creating a controller: $routes->add('acme_privacy', new Route('/privacy', [ '_controller' => 'Symfony\Bundle\FrameworkBundle\Controller\TemplateController', 'template' => 'static/privacy.html.twig', - ])); + ], [], [], '', [], ['GET'])); return $routes; @@ -88,6 +91,7 @@ exactly how your page is cached: template: 'static/privacy.html.twig' maxAge: 86400 sharedAge: 86400 + methods: GET .. code-block:: xml @@ -97,8 +101,10 @@ exactly how your page is cached: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - - Symfony\Bundle\FrameworkBundle\Controller\TemplateController + static/privacy.html.twig 86400 86400 @@ -117,7 +123,7 @@ exactly how your page is cached: 'template' => 'static/privacy.html.twig', 'maxAge' => 86400, 'sharedAge' => 86400, - ])); + ], [], [], '', [], ['GET'])); return $routes; diff --git a/translation/locale.rst b/translation/locale.rst index 2632ebcf676..1749ad67e3a 100644 --- a/translation/locale.rst +++ b/translation/locale.rst @@ -83,7 +83,7 @@ by the routing system using the special ``_locale`` parameter: http://symfony.com/schema/routing/routing-1.0.xsd"> - App\Controller\ContactContorller::index + controller="App\Controller\ContactContorller::index"> en|fr|de From 34033db9919acd7671ceb82521753f4b3c18895d Mon Sep 17 00:00:00 2001 From: Jules Pietri Date: Tue, 5 Mar 2019 01:49:25 +0100 Subject: [PATCH 2/2] used RoutingConfigurator in examples --- components/routing.rst | 100 +++++++------- controller/error_pages.rst | 17 +-- controller/service.rst | 13 +- routing.rst | 158 +++++++++++------------ routing/conditions.rst | 35 ++--- routing/custom_route_loader.rst | 53 +++++--- routing/external_resources.rst | 63 ++++----- routing/extra_information.rst | 28 ++-- routing/hostname_pattern.rst | 112 ++++++++-------- routing/optional_placeholders.rst | 50 +++---- routing/redirect_in_config.rst | 70 +++++----- routing/requirements.rst | 97 ++++++++------ routing/scheme.rst | 15 ++- routing/service_container_parameters.rst | 33 ++--- routing/slash_in_parameter.rst | 23 ++-- security.rst | 17 +-- security/form_login.rst | 16 +-- security/json_login_setup.rst | 15 ++- templating.rst | 32 ++--- templating/render_without_controller.rst | 46 ++++--- translation/locale.rst | 24 ++-- 21 files changed, 516 insertions(+), 501 deletions(-) diff --git a/components/routing.rst b/components/routing.rst index 0cb92a7ece5..1ca54d655ff 100644 --- a/components/routing.rst +++ b/components/routing.rst @@ -302,20 +302,18 @@ other loaders that work the same way: * :class:`Symfony\\Component\\Routing\\Loader\\PhpFileLoader` If you use the :class:`Symfony\\Component\\Routing\\Loader\\PhpFileLoader` you -have to provide the name of a PHP file which returns a :class:`Symfony\\Component\\Routing\\RouteCollection`:: +have to provide the name of a PHP file which returns a callable handling a :class:`Symfony\\Component\\Routing\\Loader\\Configurator\\RoutingConfigurator`. +This class allows to chain imports, collections or simple route definition calls:: // RouteProvider.php - use Symfony\Component\Routing\RouteCollection; - use Symfony\Component\Routing\Route; - - $routes = new RouteCollection(); - $routes->add( - 'route_name', - new Route('/foo', ['_controller' => 'ExampleController']) - ); - // ... + namespace Symfony\Component\Routing\Loader\Configurator; - return $routes; + return function (RoutingConfigurator $routes) { + $routes->add('route_name', '/foo') + ->controller('ExampleController') + // ... + ; + }; Routes as Closures .................. @@ -410,7 +408,7 @@ routes with UTF-8 characters: route1: path: /category/{name} - defaults: { _controller: 'App\Controller\DefaultController::category' } + controller: App\Controller\DefaultController::category options: utf8: true @@ -429,23 +427,19 @@ routes with UTF-8 characters: .. code-block:: php - use Symfony\Component\Routing\RouteCollection; - use Symfony\Component\Routing\Route; - - $routes = new RouteCollection(); - $routes->add('route1', new Route('/category/{name}', - [ - '_controller' => 'App\Controller\DefaultController::category', - ], - [], - [ - 'utf8' => true, - ] - )); + // config/routes.php + namespace Symfony\Component\Routing\Loader\Configurator; - // ... + use App\Controller\DefaultController; - return $routes; + return function (RoutingConfigurator $routes) { + $routes->add('route1', '/category/{name}') + ->controller([DefaultController::class, 'category']) + ->options([ + 'utf8' => true, + ]) + ; + }; In this route, the ``utf8`` option set to ``true`` makes Symfony consider the ``.`` requirement to match any UTF-8 characters instead of just a single @@ -470,11 +464,11 @@ You can also include UTF-8 strings as routing requirements: * @Route( * "/category/{name}", * name="route2", - * requirements={"default"="한국어"}, + * defaults={"name"="한국어"}, * options={"utf8": true} * ) */ - public function default() + public function category() { // ... } @@ -482,10 +476,10 @@ You can also include UTF-8 strings as routing requirements: .. code-block:: yaml route2: - path: /default/{default} - defaults: { _controller: 'App\Controller\DefaultController::default' } - requirements: - default: "한국어" + path: /category/{name} + controller: 'App\Controller\DefaultController::category' + defaults: + name: "한국어" options: utf8: true @@ -497,34 +491,30 @@ You can also include UTF-8 strings as routing requirements: xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - - 한국어 + + 한국어 .. code-block:: php - use Symfony\Component\Routing\RouteCollection; - use Symfony\Component\Routing\Route; - - $routes = new RouteCollection(); - $routes->add('route2', new Route('/default/{default}', - [ - '_controller' => 'App\Controller\DefaultController::default', - ], - [ - 'default' => '한국어', - ], - [ - 'utf8' => true, - ] - )); - - // ... - - return $routes; + // config/routes.php + namespace Symfony\Component\Routing\Loader\Configurator; + + use App\Controller\DefaultController; + + return function (RoutingConfigurator $routes) { + $routes->add('route2', '/category/{name}') + ->controller([DefaultController::class, 'category']) + ->defaults([ + 'name' => '한국어', + ]) + ->options([ + 'utf8' => true, + ]) + ; + }; .. tip:: diff --git a/controller/error_pages.rst b/controller/error_pages.rst index 7fa444003be..bf551dda2e4 100644 --- a/controller/error_pages.rst +++ b/controller/error_pages.rst @@ -171,22 +171,19 @@ automatically when installing Twig support): xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - + .. code-block:: php // config/routes/dev/twig.php - use Symfony\Component\Routing\RouteCollection; + namespace Symfony\Component\Routing\Loader\Configurator; - $routes = new RouteCollection(); - $routes->addCollection( - $loader->import('@TwigBundle/Resources/config/routing/errors.xml') - ); - $routes->addPrefix("/_error"); - - return $routes; + return function (RoutingConfigurator $routes) { + $routes->import('@TwigBundle/Resources/config/routing/errors.xml') + ->prefix('/_error') + ; + }; With this route added, you can use URLs like these to preview the *error* page for a given status code as HTML or for a given status code and format. diff --git a/controller/service.rst b/controller/service.rst index 082feb6cfc7..55c3d71aaef 100644 --- a/controller/service.rst +++ b/controller/service.rst @@ -64,9 +64,16 @@ a service like: ``App\Controller\HelloController::index``: .. code-block:: php // config/routes.php - $collection->add('hello', new Route('/hello', [ - '_controller' => 'App\Controller\HelloController::index', - ], [], [], '', [], ['GET'])); + namespace Symfony\Component\Routing\Loader\Configurator; + + use App\Controller\HelloController; + + return function (RoutingConfigurator $routes) { + $routes->add('hello', '/hello') + ->controller(['HelloController::class, 'index']) + ->methods(['GET']) + ; + }; .. _controller-service-invoke: diff --git a/routing.rst b/routing.rst index f77081f9bec..10e708d829e 100644 --- a/routing.rst +++ b/routing.rst @@ -102,19 +102,18 @@ Now you can configure the routes: .. code-block:: php // config/routes.php - use App\Controller\BlogController; - use Symfony\Component\Routing\RouteCollection; - use Symfony\Component\Routing\Route; + namespace Symfony\Component\Routing\Loader\Configurator; - $routes = new RouteCollection(); - $routes->add('blog_list', new Route('/blog', [ - '_controller' => [BlogController::class, 'list'] - ])); - $routes->add('blog_show', new Route('/blog/{slug}', [ - '_controller' => [BlogController::class, 'show'] - ])); + use App\Controller\BlogController; - return $routes; + return function (RoutingConfigurator $routes) { + $routes->add('blog_list', '/blog') + ->controller([BlogController::class, 'list']) + ; + $routes->add('blog_show', '/blog/{slug}') + ->controller([BlogController::class, 'show']) + ; + }; Thanks to these two routes: @@ -202,9 +201,11 @@ Symfony provides a handy way to declare localized routes without duplication. // config/routes.php namespace Symfony\Component\Routing\Loader\Configurator; + use App\Controller\CompanyController; + return function (RoutingConfigurator $routes) { $routes->add('about_us', ['nl' => '/over-ons', 'en' => '/about-us']) - ->controller('App\Controller\CompanyController::about'); + ->controller([CompanyController::class, 'about']); }; When a localized route is matched Symfony automatically knows which locale @@ -254,15 +255,17 @@ with a locale. This can be done by defining a different prefix for each locale .. code-block:: php // config/routes/annotations.php - use Symfony\Component\Routing\RouteCollection; - - $routes = $loader->import('../src/Controller/', 'annotation'); - - // don't prefix URLs for English, the default locale - $app->addPrefix('/', ['_locale' => 'en']); - $app->addPrefix('/nl', ['_locale' => 'nl']); + namespace Symfony\Component\Routing\Loader\Configurator; - return $routes; + return function (RoutingConfigurator $routes) { + $routes->import('../src/Controller/', 'annotation') + ->prefix([ + // don't prefix URLs for English, the default locale + 'en' => '', + 'nl' => '/nl' + ]) + ; + }; .. _routing-requirements: @@ -342,20 +345,17 @@ To fix this, add a *requirement* that the ``{page}`` wildcard can *only* match n .. code-block:: php // config/routes.php - use Symfony\Component\Routing\RouteCollection; - use Symfony\Component\Routing\Route; - use App\Controller\BlogController; - - $routes = new RouteCollection(); - $routes->add('blog_list', new Route('/blog/{page}', [ - '_controller' => [BlogController::class, 'list'], - ], [ - 'page' => '\d+' - ])); + namespace Symfony\Component\Routing\Loader\Configurator; - // ... + use App\Controller\BlogController; - return $routes; + return function (RoutingConfigurator $routes) { + $routes->add('blog_list', '/blog/{page}') + ->controller([BlogController::class, 'list']) + ->requirements(['page' => '\d+']) + ; + // ... + }; The ``\d+`` is a regular expression that matches a *digit* of any length. Now: @@ -415,18 +415,16 @@ concise, but it can decrease route readability when requirements are complex: .. code-block:: php // config/routes.php - use Symfony\Component\Routing\RouteCollection; - use Symfony\Component\Routing\Route; - use App\Controller\BlogController; - - $routes = new RouteCollection(); - $routes->add('blog_list', new Route('/blog/{page<\d+>}', [ - '_controller' => [BlogController::class, 'list'], - ])); + namespace Symfony\Component\Routing\Loader\Configurator; - // ... + use App\Controller\BlogController; - return $routes; + return function (RoutingConfigurator $routes) { + $routes->add('blog_list', '/blog/{page<\d+>}') + ->controller([BlogController::class, 'list']) + ; + // ... + }; To learn about other route requirements - like HTTP method, hostname and dynamic expressions - see :doc:`/routing/requirements`. @@ -498,25 +496,17 @@ So how can you make ``blog_list`` once again match when the user visits .. code-block:: php // config/routes.php - use Symfony\Component\Routing\RouteCollection; - use Symfony\Component\Routing\Route; - use App\Controller\BlogController; - - $routes = new RouteCollection(); - $routes->add('blog_list', new Route( - '/blog/{page}', - [ - '_controller' => [BlogController::class, 'list'], - 'page' => 1, - ], - [ - 'page' => '\d+' - ] - )); + namespace Symfony\Component\Routing\Loader\Configurator; - // ... + use App\Controller\BlogController; - return $routes; + return function (RoutingConfigurator $routes) { + $routes->add('blog_list', '/blog/{page}') + ->controller([BlogController::class, 'list']) + ->defaults(['page' => 1]) + ->requirements(['page' => '\d+']) + ; + }; Now, when the user visits ``/blog``, the ``blog_list`` route will match and ``$page`` will default to a value of ``1``. @@ -579,18 +569,15 @@ placeholder: .. code-block:: php // config/routes.php - use Symfony\Component\Routing\RouteCollection; - use Symfony\Component\Routing\Route; - use App\Controller\BlogController; - - $routes = new RouteCollection(); - $routes->add('blog_list', new Route('/blog/{page<\d+>?1}', [ - '_controller' => [BlogController::class, 'list'], - ])); + namespace Symfony\Component\Routing\Loader\Configurator; - // ... + use App\Controller\BlogController; - return $routes; + return function (RoutingConfigurator $routes) { + $routes->add('blog_list', '/blog/{page<\d+>?1') + ->controller([BlogController::class, 'list']) + ; + }; .. tip:: @@ -686,24 +673,23 @@ With all of this in mind, check out this advanced example: .. code-block:: php // config/routes.php - use Symfony\Component\Routing\RouteCollection; - use Symfony\Component\Routing\Route; + namespace Symfony\Component\Routing\Loader\Configurator; + use App\Controller\ArticleController; - $routes = new RouteCollection(); - $routes->add( - 'article_show', - new Route('/articles/{_locale}/{year}/{slug}.{_format}', [ - '_controller' => [ArticleController::class, 'show'], - '_format' => 'html', - ], [ - '_locale' => 'en|fr', - '_format' => 'html|rss', - 'year' => '\d+', - ]) - ); - - return $routes; + return function (RoutingConfigurator $routes) { + $routes->add('article_show', '/articles/{_locale}/{year}/{slug}.{_format}') + ->controller([ArticleController::class, 'show']) + ->defaults([ + '_format' => 'html', + ]) + ->requirements([ + '_locale' => 'en|fr', + '_format' => 'html|rss', + 'year' => '\d+', + ]) + ; + }; As you've seen, this route will only match if the ``{_locale}`` portion of the URL is either ``en`` or ``fr`` and if the ``{year}`` is a number. This @@ -812,7 +798,7 @@ To generate a URL, you need to specify the name of the route (e.g. ``blog_show`` and any wildcards (e.g. ``slug = my-blog-post``) used in the path for that route. With this information, an URL can be generated in a controller:: - class MainController extends AbstractController + class BlogController extends AbstractController { public function show($slug) { diff --git a/routing/conditions.rst b/routing/conditions.rst index c98b62767ab..6040b2bb994 100644 --- a/routing/conditions.rst +++ b/routing/conditions.rst @@ -55,9 +55,7 @@ define arbitrary matching logic, use the ``conditions`` routing option: xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - + context.getMethod() in ['GET', 'HEAD'] and request.headers.get('User-Agent') matches '/firefox/i' @@ -67,25 +65,18 @@ define arbitrary matching logic, use the ``conditions`` routing option: .. code-block:: php // config/routes.php - use Symfony\Component\Routing\RouteCollection; - use Symfony\Component\Routing\Route; - - $routes = new RouteCollection(); - $routes->add('contact', new Route( - '/contact', [ - '_controller' => 'App\Controller\DefaultController::contact', - ], - [], - [], - '', - [], - [], - 'context.getMethod() in ["GET", "HEAD"] and request.headers.get("User-Agent") matches "/firefox/i"' - // expressions can also include config parameters - // 'request.headers.get("User-Agent") matches "%app.allowed_browsers%"' - )); - - return $collection; + namespace Symfony\Component\Routing\Loader\Configurator; + + use App\Controller\DefaultController; + + return function (RoutingConfigurator $routes) { + $routes->add('contact', '') + ->controller([DefaultController::class, 'contact']) + ->condition('context.getMethod() in ["GET", "HEAD"] and request.headers.get("User-Agent") matches "/firefox/i"') + // expressions can also include config parameters + // 'request.headers.get("User-Agent") matches "%app.allowed_browsers%"' + ; + }; The ``condition`` is an expression, and you can learn more about its syntax here: :doc:`/components/expression_language/syntax`. With this, the route diff --git a/routing/custom_route_loader.rst b/routing/custom_route_loader.rst index e2942ca0839..eb00b2a8f14 100644 --- a/routing/custom_route_loader.rst +++ b/routing/custom_route_loader.rst @@ -38,12 +38,35 @@ and :method:`Symfony\\Component\\Config\\Loader\\LoaderInterface::load`. Take these lines from the ``routes.yaml``: -.. code-block:: yaml +.. configuration-block:: + + .. code-block:: yaml + + # config/routes.yaml + controllers: + resource: ../src/Controller/ + type: annotation + + .. code-block:: xml + + + + - # config/routes.yaml - controllers: - resource: ../src/Controller/ - type: annotation + + + + .. code-block:: php + + // config/routes.php + namespace Symfony\Component\Routing\Loader\Configurator; + + return function (RoutingConfigurator $routes) { + $routes->import('../src/Controller', 'annotation'); + }; When the main loader parses this, it tries all registered delegate loaders and calls their :method:`Symfony\\Component\\Config\\Loader\\LoaderInterface::supports` @@ -93,14 +116,11 @@ and configure the service and method to call: .. code-block:: php // config/routes.php - use Symfony\Component\Routing\RouteCollection; + namespace Symfony\Component\Routing\Loader\Configurator; - $routes = new RouteCollection(); - $routes->addCollection( - $loader->import("admin_route_loader:loadRoutes", "service") - ); - - return $routes; + return function (RoutingConfigurator $routes) { + $routes->import('admin_route_loader:loadRoutes', 'service'); + }; In this example, the routes are loaded by calling the ``loadRoutes()`` method of the service whose ID is ``admin_route_loader``. Your service doesn't have to @@ -260,12 +280,11 @@ What remains to do is adding a few lines to the routing configuration: .. code-block:: php // config/routes.php - use Symfony\Component\Routing\RouteCollection; - - $routes = new RouteCollection(); - $routes->addCollection($loader->import('.', 'extra')); + namespace Symfony\Component\Routing\Loader\Configurator; - return $routes; + return function (RoutingConfigurator $routes) { + $routes->import('.', 'extra'); + }; The important part here is the ``type`` key. Its value should be ``extra`` as this is the type which the ``ExtraLoader`` supports and this will make sure diff --git a/routing/external_resources.rst b/routing/external_resources.rst index dc6e5cb9694..9cf97a33068 100644 --- a/routing/external_resources.rst +++ b/routing/external_resources.rst @@ -61,24 +61,21 @@ This can be done by importing routing resources from the main routing file: .. code-block:: php // config/routes.php - use Symfony\Component\Routing\RouteCollection; + namespace Symfony\Component\Routing\Loader\Configurator; - $routes = new RouteCollection(); - $routes->addCollection( + return function (RoutingConfigurator $routes) { // loads routes from the given routing file stored in some bundle - $loader->import("@AcmeOtherBundle/Resources/config/routing.yaml") + $routes->import('@AcmeOtherBundle/Resources/config/routing.yaml'); // loads routes from the PHP annotations of the controllers found in that directory - $loader->import("../src/Controller/", "annotation") + $routes->import('../src/Controller/', 'annotation'); // loads routes from the YAML or XML files found in that directory - $loader->import("../legacy/routing/", "directory") + $routes->import('../legacy/routing/', 'directory'); // loads routes from the YAML or XML files found in some bundle directory - $loader->import("@AppBundle/Resources/config/routing/public/", "directory") - ); - - return $routes; + $routes->import('@AppBundle/Resources/config/routing/public/', 'directory'); + }; .. note:: @@ -125,24 +122,19 @@ suppose you want to prefix all application routes with ``/site`` (e.g. xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - + .. code-block:: php // config/routes.php - use Symfony\Component\Routing\RouteCollection; - - $app = $loader->import('../src/Controller/', 'annotation'); - $app->addPrefix('/site'); + namespace Symfony\Component\Routing\Loader\Configurator; - $routes = new RouteCollection(); - $routes->addCollection($app); - - return $routes; + return function (RoutingConfigurator $routes) { + $routes->import('../src/Controller/', 'annotation') + ->prefix('/site') + ; + }; The path of each route being loaded from the new routing resource will now be prefixed with the string ``/site``. @@ -184,12 +176,15 @@ be prefixed with the string ``/site``. .. code-block:: php // config/routes.php - use Symfony\Component\Routing\RouteCollection; + namespace Symfony\Component\Routing\Loader\Configurator; - $app = $loader->import('../src/Controller/', 'annotation'); - // the second argument is the $trailingSlashOnRoot option - $app->addPrefix('/site', false); - // ... + use App\Controller\ArticleController; + + return function (RoutingConfigurator $routes) { + $routes->import('../src/Controller/', 'annotation') + ->prefix('/site', false) + ; + }; Prefixing the Names of Imported Routes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -251,15 +246,13 @@ a controller class or imported from a configuration file: .. code-block:: php // config/routes.php - use Symfony\Component\Routing\RouteCollection; - - $app = $loader->import('../src/Controller/', 'annotation'); - $app->addNamePrefix('blog_'); - - $collection = new RouteCollection(); - $collection->addCollection($app); + namespace Symfony\Component\Routing\Loader\Configurator; - return $collection; + return function (RoutingConfigurator $routes) { + $routes->import('../src/Controller/', 'annotation') + ->namePrefix('blog_') + ; + }; In this example, the names of the routes will be ``blog_index`` and ``blog_post``. diff --git a/routing/extra_information.rst b/routing/extra_information.rst index 5fc3d144b38..d10b82c863e 100644 --- a/routing/extra_information.rst +++ b/routing/extra_information.rst @@ -30,9 +30,7 @@ to your controller, and as attributes of the ``Request`` object: xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - + 1 Hello world! @@ -41,17 +39,19 @@ to your controller, and as attributes of the ``Request`` object: .. code-block:: php // config/routes.php - use Symfony\Component\Routing\RouteCollection; - use Symfony\Component\Routing\Route; - - $routes = new RouteCollection(); - $routes->add('blog', new Route('/blog/{page}', [ - '_controller' => 'App\Controller\BlogController::index', - 'page' => 1, - 'title' => 'Hello world!', - ])); - - return $routes; + namespace Symfony\Component\Routing\Loader\Configurator; + + use App\Controller\BlogController; + + return function (RoutingConfigurator $routes) { + $routes->add('blog', '/blog/{page}') + ->controller([BlogController::class, 'index']) + ->defaults([ + 'page' => 1, + 'title' => 'Hello world!', + ]) + ; + }; Now, you can access this extra parameter in your controller, as an argument to the controller method:: diff --git a/routing/hostname_pattern.rst b/routing/hostname_pattern.rst index ec9d51d5590..2d341aa3c72 100644 --- a/routing/hostname_pattern.rst +++ b/routing/hostname_pattern.rst @@ -67,17 +67,19 @@ You can also match on the HTTP *host* of the incoming request. .. code-block:: php // config/routes.php - use Symfony\Component\Routing\RouteCollection; - use Symfony\Component\Routing\Route; + namespace Symfony\Component\Routing\Loader\Configurator; - $routes = new RouteCollection(); - $routes->add('mobile_homepage', new Route('/', [ - '_controller' => 'App\Controller\MainController::mobileHomepage', - ], [], [], 'm.example.com')); + use App\Controller\MainController; - $routes->add('homepage', new Route('/', [ - '_controller' => 'App\Controller\MainController::homepage', - ])); + return function (RoutingConfigurator $routes) { + $routes->add('mobile_homepage', '/') + ->controller([MainController::class, 'mobileHomepage']) + ->host('m.example.com') + ; + $routes->add('homepage', '/') + ->controller([MainController::class, 'homepage']) + ; + }; return $routes; @@ -151,19 +153,19 @@ you can use placeholders in your hostname: .. code-block:: php // config/routes.php - use Symfony\Component\Routing\RouteCollection; - use Symfony\Component\Routing\Route; + namespace Symfony\Component\Routing\Loader\Configurator; - $routes = new RouteCollection(); - $routes->add('project_homepage', new Route('/', [ - '_controller' => 'App\Controller\MainController::projectsHomepage', - ], [], [], '{project}.example.com')); + use App\Controller\MainController; - $routes->add('homepage', new Route('/', [ - '_controller' => 'App\Controller\MainController::homepage', - ])); - - return $routes; + return function (RoutingConfigurator $routes) { + $routes->add('project_homepage', '/') + ->controller([MainController::class, 'projectHomepage']) + ->host('{project}.example.com') + ; + $routes->add('homepage', '/') + ->controller([MainController::class, 'homepage']) + ; + }; You can also set requirements and default options for these placeholders. For instance, if you want to match both ``m.example.com`` and @@ -243,22 +245,25 @@ instance, if you want to match both ``m.example.com`` and .. code-block:: php // config/routes.php - use Symfony\Component\Routing\RouteCollection; - use Symfony\Component\Routing\Route; - - $routes = new RouteCollection(); - $routes->add('mobile_homepage', new Route('/', [ - '_controller' => 'App\Controller\MainController::mobileHomepage', - 'subdomain' => 'm', - ], [ - 'subdomain' => 'm|mobile', - ], [], '{subdomain}.example.com')); - - $routes->add('homepage', new Route('/', [ - '_controller' => 'App\Controller\MainController::homepage', - ])); - - return $routes; + namespace Symfony\Component\Routing\Loader\Configurator; + + use App\Controller\MainController; + + return function (RoutingConfigurator $routes) { + $routes->add('mobile_homepage', '/') + ->controller([MainController::class, 'mobileHomepage']) + ->host('{subdomain}.example.com') + ->defaults([ + 'subdomain' => 'm', + ]) + ->requirements([ + 'subdomain' => 'm|mobile', + ]) + ; + $routes->add('homepage', '/') + ->controller([MainController::class, 'homepage']) + ; + }; .. tip:: @@ -339,22 +344,25 @@ instance, if you want to match both ``m.example.com`` and .. code-block:: php // config/routes.php - use Symfony\Component\Routing\RouteCollection; - use Symfony\Component\Routing\Route; - - $routes = new RouteCollection(); - $routes->add('mobile_homepage', new Route('/', [ - '_controller' => 'App\Controller\MainController::mobileHomepage', - 'domain' => '%domain%', - ], [ - 'domain' => '%domain%', - ], [], 'm.{domain}')); - - $routes->add('homepage', new Route('/', [ - '_controller' => 'App\Controller\MainController::homepage', - ])); - - return $routes; + namespace Symfony\Component\Routing\Loader\Configurator; + + use App\Controller\MainController; + + return function (RoutingConfigurator $routes) { + $routes->add('mobile_homepage', '/') + ->controller([MainController::class, 'mobileHomepage']) + ->host('m.{domain}') + ->defaults([ + 'domain' => '%domain%', + ]) + ->requirements([ + 'domain' => '%domain%', + ]) + ; + $routes->add('homepage', '/') + ->controller([MainController::class, 'homepage']) + ; + }; .. tip:: diff --git a/routing/optional_placeholders.rst b/routing/optional_placeholders.rst index 4475bfcc179..fda8ffbd2ed 100644 --- a/routing/optional_placeholders.rst +++ b/routing/optional_placeholders.rst @@ -49,15 +49,15 @@ the available blog posts for this imaginary blog application: .. code-block:: php // config/routes.php - use Symfony\Component\Routing\RouteCollection; - use Symfony\Component\Routing\Route; + namespace Symfony\Component\Routing\Loader\Configurator; - $routes = new RouteCollection(); - $routes->add('blog', new Route('/blog', [ - '_controller' => 'App\Controller\BlogController::index', - ])); + use App\Controller\BlogController; - return $routes; + return function (RoutingConfigurator $routes) { + $routes->add('blog', '/blog') + ->controller([BlogController::class, 'index']) + ; + }; So far, this route is as simple as possible - it contains no placeholders and will only match the exact URL ``/blog``. But what if you need this route @@ -102,15 +102,15 @@ entries? Update the route to have a new ``{page}`` placeholder: .. code-block:: php // config/routes.php - use Symfony\Component\Routing\RouteCollection; - use Symfony\Component\Routing\Route; + namespace Symfony\Component\Routing\Loader\Configurator; - $routes = new RouteCollection(); - $routes->add('blog', new Route('/blog/{page}', [ - '_controller' => 'App\Controller\BlogController::index', - ])); + use App\Controller\BlogController; - return $routes; + return function (RoutingConfigurator $routes) { + $routes->add('blog', '/blog/{page}') + ->controller([BlogController::class, 'index']) + ; + }; Like the ``{slug}`` placeholder before, the value matching ``{page}`` will be available inside your controller. Its value can be used to determine which @@ -163,16 +163,18 @@ This is done by including it in the ``defaults`` collection: .. code-block:: php // config/routes.php - use Symfony\Component\Routing\RouteCollection; - use Symfony\Component\Routing\Route; - - $routes = new RouteCollection(); - $routes->add('blog', new Route('/blog/{page}', [ - '_controller' => 'App\Controller\BlogController::index', - 'page' => 1, - ])); - - return $routes; + namespace Symfony\Component\Routing\Loader\Configurator; + + use App\Controller\BlogController; + + return function (RoutingConfigurator $routes) { + $routes->add('blog', '/blog/{page}') + ->controller([BlogController::class, 'index']) + ->defaults([ + 'page' => 1, + ]) + ; + }; By adding ``page`` to the ``defaults`` key, the ``{page}`` placeholder is no longer required. The URL ``/blog`` will match this route and the value diff --git a/routing/redirect_in_config.rst b/routing/redirect_in_config.rst index beb9e45c841..565c4882ddc 100644 --- a/routing/redirect_in_config.rst +++ b/routing/redirect_in_config.rst @@ -64,25 +64,24 @@ action to redirect to this new url: .. code-block:: php // config/routes.php - use Symfony\Component\Routing\RouteCollection; - use Symfony\Component\Routing\Route; - - $routes = new RouteCollection(); - - // load some routes - one should ultimately have the path "/app" - $appRoutes = $loader->import("../src/Controller/", "annotation"); - $appRoutes->setPrefix('/app'); - - $routes->addCollection($appRoutes); - - // redirecting the homepage - $routes->add('homepage', new Route('/', [ - '_controller' => 'Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction', - 'path' => '/app', - 'permanent' => true, - ])); - - return $routes; + namespace Symfony\Component\Routing\Loader\Configurator; + + use Symfony\Bundle\FrameworkBundle\Controller\RedirectController; + + return function (RoutingConfigurator $routes) { + // load some routes - one should ultimately have the path "/app" + $routes->import('../src/Controller/', 'annotation') + ->prefix('/app') + ; + // redirecting the homepage + $routes->add('homepage', '/') + ->controller([RedirectController::class, 'urlRedirectAction']) + ->defaults([ + 'path' => '/app', + 'permanent' => true, + ]) + ; + }; In this example, you configured a route for the ``/`` path and let the ``RedirectController`` redirect it to ``/app``. The ``permanent`` switch @@ -141,22 +140,23 @@ action: .. code-block:: php // config/routes.php - use Symfony\Component\Routing\RouteCollection; - use Symfony\Component\Routing\Route; - - $routes = new RouteCollection(); - // ... - - $routes->add('admin', new Route('/wp-admin', [ - '_controller' => 'Symfony\Bundle\FrameworkBundle\Controller\RedirectController::redirectAction', - 'route' => 'sonata_admin_dashboard', - // make a permanent redirection... - 'permanent' => true, - // ...and keep the original query string parameters - 'keepQueryParams' => true, - ])); - - return $routes; + namespace Symfony\Component\Routing\Loader\Configurator; + + use Symfony\Bundle\FrameworkBundle\Controller\RedirectController; + + return function (RoutingConfigurator $routes) { + // redirecting the homepage + $routes->add('admin', '/wp-admin') + ->controller([RedirectController::class, 'redirectAction']) + ->defaults([ + 'route' => 'sonata_admin_dashboard', + // make a permanent redirection... + 'permanent' => true, + // ...and keep the original query string parameters + 'keepQueryParams' => true, + ]) + ; + }; .. caution:: diff --git a/routing/requirements.rst b/routing/requirements.rst index 4e4e05e486c..c10761e4239 100644 --- a/routing/requirements.rst +++ b/routing/requirements.rst @@ -47,9 +47,7 @@ a routing ``{wildcard}`` to only match some regular expression: xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - + \d+ @@ -59,19 +57,19 @@ a routing ``{wildcard}`` to only match some regular expression: .. code-block:: php // config/routes.php - use Symfony\Component\Routing\RouteCollection; - use Symfony\Component\Routing\Route; + namespace Symfony\Component\Routing\Loader\Configurator; - $routes = new RouteCollection(); - $routes->add('blog_list', new Route('/blog/{page}', [ - '_controller' => 'App\Controller\BlogController::list', - ], [ - 'page' => '\d+', - ])); + use App\Controller\BlogController; - // ... - - return $routes; + return function (RoutingConfigurator $routes) { + $routes->add('blog_list', '/blog/{page}') + ->controller([BlogController::class, 'list']) + ->requirements([ + 'page' => '\d+', + ]) + ; + // ... + }; Thanks to the ``\d+`` requirement (i.e. a "digit" of any length), ``/blog/2`` will match this route but ``/blog/some-string`` will *not* match. @@ -126,9 +124,7 @@ URL: xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - + en en|fr @@ -137,18 +133,21 @@ URL: .. code-block:: php // config/routes.php - use Symfony\Component\Routing\RouteCollection; - use Symfony\Component\Routing\Route; - - $routes = new RouteCollection(); - $routes->add('homepage', new Route('/{_locale}', [ - '_controller' => 'App\Controller\MainController::homepage', - '_locale' => 'en', - ], [ - '_locale' => 'en|fr', - ])); - - return $routes; + namespace Symfony\Component\Routing\Loader\Configurator; + + use App\Controller\MainController; + + return function (RoutingConfigurator $routes) { + $routes->add('homepage', '/{_locale}') + ->controller([MainController::class, 'homepage']) + ->defaults([ + '_locale' => 'en', + ]) + ->requirements([ + '_locale' => 'en|fr', + ]) + ; + }; For incoming requests, the ``{_locale}`` portion of the URL is matched against the regular expression ``(en|fr)``. @@ -253,19 +252,33 @@ accomplished with the following route configuration: .. code-block:: php // config/routes.php - use Symfony\Component\Routing\RouteCollection; - use Symfony\Component\Routing\Route; - - $routes = new RouteCollection(); - $routes->add('api_post_show', new Route('/api/posts/{id}', [ - '_controller' => 'App\Controller\BlogApiController::show', - ], [], [], '', [], ['GET', 'HEAD'])); - - $routes->add('api_post_edit', new Route('/api/posts/{id}', [ - '_controller' => 'App\Controller\BlogApiController::edit', - ], [], [], '', [], ['PUT'])); - - return $routes; + namespace Symfony\Component\Routing\Loader\Configurator; + + use App\Controller\BlogApiController; + + return function (RoutingConfigurator $routes) { + $routes->add('api_post_show', '/api/posts/{id}') + ->controller([BlogApiController::class, 'show']) + ->methods(['GET', 'HEAD']) + ; + $routes->add('api_post_edit', '/api/posts/{id}') + ->controller([BlogApiController::class, 'edit']) + ->methods(['PUT']) + ; + + // or use collection + $api = $routes->collection('api_post_') + ->prefix('/api/posts/{id}') + ; + $api->add('show') + ->controller([BlogApiController::class, 'show']) + ->methods(['GET', 'HEAD']) + ; + $api->add('edit') + ->controller([BlogApiController::class, 'edit']) + ->methods(['PUT']) + ; + }; Despite the fact that these two routes have identical paths (``/api/posts/{id}``), the first route will match only GET or HEAD requests and diff --git a/routing/scheme.rst b/routing/scheme.rst index ef662191eba..25716e20d08 100644 --- a/routing/scheme.rst +++ b/routing/scheme.rst @@ -52,15 +52,16 @@ the URI scheme via schemes: .. code-block:: php // config/routes.php - use Symfony\Component\Routing\RouteCollection; - use Symfony\Component\Routing\Route; + namespace Symfony\Component\Routing\Loader\Configurator; - $routes = new RouteCollection(); - $routes->add('secure', new Route('/secure', [ - '_controller' => 'App\Controller\MainController::secure', - ], [], [], '', ['https'])); + use App\Controller\MainController; - return $routes; + return function (RoutingConfigurator $routes) { + $routes->add('secure', '/secure') + ->controller([MainController::class, 'secure']) + ->schemes(['https']) + ; + }; The above configuration forces the ``secure`` route to always use HTTPS. diff --git a/routing/service_container_parameters.rst b/routing/service_container_parameters.rst index ebe325ae21e..a137619b9f8 100644 --- a/routing/service_container_parameters.rst +++ b/routing/service_container_parameters.rst @@ -42,17 +42,18 @@ inside your routing configuration: .. code-block:: php // config/routes.php - use Symfony\Component\Routing\RouteCollection; - use Symfony\Component\Routing\Route; + namespace Symfony\Component\Routing\Loader\Configurator; - $routes = new RouteCollection(); - $routes->add('contact', new Route('/{_locale}/contact', [ - '_controller' => 'App\Controller\MainController::contact', - ), [ - '_locale' => '%app.locales%', - ])); + use App\Controller\MainController; - return $routes; + return function (RoutingConfigurator $routes) { + $routes->add('contact', '/{_locale}/contact') + ->controller([MainController::class, 'contact']) + ->requirements([ + '_locale' => '%app.locales%', + ]) + ; + }; You can now control and set the ``app.locales`` parameter somewhere in your container: @@ -113,15 +114,15 @@ path): .. code-block:: php // config/routes.php - use Symfony\Component\Routing\RouteCollection; - use Symfony\Component\Routing\Route; + namespace Symfony\Component\Routing\Loader\Configurator; - $routes = new RouteCollection(); - $routes->add('some_route', new Route('/%app.route_prefix%/contact', [ - '_controller' => 'App\Controller\MainController::contact', - ])); + use App\Controller\MainController; - return $routes; + return function (RoutingConfigurator $routes) { + $routes->add('contact', '/%app.route_prefix%/contact') + ->controller([MainController::class, 'contact']) + ; + }; .. note:: diff --git a/routing/slash_in_parameter.rst b/routing/slash_in_parameter.rst index f1b767942f3..a2aecc57b4e 100644 --- a/routing/slash_in_parameter.rst +++ b/routing/slash_in_parameter.rst @@ -65,17 +65,18 @@ a more permissive regular expression for it: .. code-block:: php // config/routes.php - use Symfony\Component\Routing\RouteCollection; - use Symfony\Component\Routing\Route; - - $routes = new RouteCollection(); - $routes->add('share', new Route('/share/{token}', [ - '_controller' => 'App\Controller\DefaultController::share', - ], [ - 'token' => '.+', - ])); - - return $routes; + namespace Symfony\Component\Routing\Loader\Configurator; + + use App\Controller\DefaultController; + + return function (RoutingConfigurator $routes) { + $routes->add('share', '/share/{token}') + ->controller([DefaultController::class, 'share']) + ->requirements([ + 'token' => '.+', + ]) + ; + }; That's it! Now, the ``{token}`` parameter can contain the ``/`` character. diff --git a/security.rst b/security.rst index a6a7a9ab8bf..a5b81aca4d1 100644 --- a/security.rst +++ b/security.rst @@ -800,6 +800,7 @@ Next, you'll need to create a route for this URL (but not a controller): # config/routes.yaml app_logout: path: /logout + methods: GET .. code-block:: php-annotations @@ -812,7 +813,7 @@ Next, you'll need to create a route for this URL (but not a controller): class SecurityController extends AbstractController { /** - * @Route("/logout", name="app_logout") + * @Route("/logout", name="app_logout", methods={"GET"}) */ public function logout() { @@ -830,19 +831,19 @@ Next, you'll need to create a route for this URL (but not a controller): xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - + .. code-block:: php // config/routes.php - use Symfony\Component\Routing\RouteCollection; - use Symfony\Component\Routing\Route; + namespace Symfony\Component\Routing\Loader\Configurator; - $routes = new RouteCollection(); - $routes->add('app_logout', new Route('/logout')); - - return $routes; + return function (RoutingConfigurator $routes) { + $routes->add('logout', '/logout') + ->methods(['GET']) + ; + }; And that's it! By sending a user to the ``app_logout`` route (i.e. to ``/logout``) Symfony will un-authenticate the current user and redirect them. diff --git a/security/form_login.rst b/security/form_login.rst index 43fb8567a6c..fccaca171e3 100644 --- a/security/form_login.rst +++ b/security/form_login.rst @@ -130,16 +130,16 @@ configuration (``login``): .. code-block:: php // config/routes.php - use App\Controller\SecurityController; - use Symfony\Component\Routing\RouteCollection; - use Symfony\Component\Routing\Route; + namespace Symfony\Component\Routing\Loader\Configurator; - $routes = new RouteCollection(); - $routes->add('login', new Route('/login', [ - '_controller' => [SecurityController::class, 'login'], - ], [], [], '', ['GET', 'POST'])); + use App\Controller\SecurityController; - return $routes; + return function (RoutingConfigurator $routes) { + $routes->add('login', '/login') + ->controller([SecurityController::class, 'login']) + ->methods(['GET', 'POST']) + ; + }; Great! Next, add the logic to ``login()`` that displays the login form:: diff --git a/security/json_login_setup.rst b/security/json_login_setup.rst index 2ef3236ddec..47b27e3d90b 100644 --- a/security/json_login_setup.rst +++ b/security/json_login_setup.rst @@ -110,15 +110,16 @@ The next step is to configure a route in your app matching this path: .. code-block:: php // config/routes.php - use Symfony\Component\Routing\RouteCollection; - use Symfony\Component\Routing\Route; + namespace Symfony\Component\Routing\Loader\Configurator; - $routes = new RouteCollection(); - $routes->add('login', new Route('/login', [ - '_controller' => 'App\Controller\SecurityController::login', - ], [], [], '', [], ['POST'])); + use App\Controller\SecurityController; - return $routes; + return function (RoutingConfigurator $routes) { + $routes->add('login', '/login') + ->controller([SecurityController::class, 'login']) + ->methods(['POST']) + ; + }; Now, when you make a ``POST`` request, with the header ``Content-Type: application/json``, to the ``/login`` URL with the following JSON document as the body, the security diff --git a/templating.rst b/templating.rst index 326bca3e545..870a79101f2 100644 --- a/templating.rst +++ b/templating.rst @@ -530,15 +530,16 @@ configuration: .. code-block:: php // config/routes.php - use Symfony\Component\Routing\Route; - use Symfony\Component\Routing\RouteCollection; + namespace Symfony\Component\Routing\Loader\Configurator; - $routes = new RouteCollection(); - $routes->add('welcome', new Route('/', [ - '_controller' => 'App\Controller\WelcomeController::index', - ], [], [], '', [], ['GET'])); + use App\Controller\WelcomeController; - return $routes; + return function (RoutingConfigurator $routes) { + $routes->add('welcome', '/') + ->controller([WelcomeController::class, 'index']) + ->methods(['GET']) + ; + }; To link to the page, use the ``path()`` Twig function and refer to the route: @@ -575,7 +576,7 @@ route: article_show: path: /article/{slug} controller: App\Controller\ArticleController::show - methods: 'GET' + methods: GET .. code-block:: xml @@ -595,15 +596,16 @@ route: .. code-block:: php // config/routes.php - use Symfony\Component\Routing\Route; - use Symfony\Component\Routing\RouteCollection; + namespace Symfony\Component\Routing\Loader\Configurator; - $routes = new RouteCollection(); - $routes->add('article_show', new Route('/article/{slug}', [ - '_controller' => 'App\Controller\ArticleController::show', - ], [], [], '', [], ['GET'])); + use App\Controller\ArticleController; - return $routes; + return function (RoutingConfigurator $routes) { + $routes->add('article_show', '/articles/{slug}') + ->controller([ArticleController::class, 'show']) + ->methods(['GET']) + ; + }; In this case, you need to specify both the route name (``article_show``) and a value for the ``{slug}`` parameter. Using this route, revisit the diff --git a/templating/render_without_controller.rst b/templating/render_without_controller.rst index fe5ed082593..377f0ab5053 100644 --- a/templating/render_without_controller.rst +++ b/templating/render_without_controller.rst @@ -46,16 +46,19 @@ can do this without creating a controller: .. code-block:: php // config/routes.php - use Symfony\Component\Routing\RouteCollection; - use Symfony\Component\Routing\Route; + namespace Symfony\Component\Routing\Loader\Configurator; - $routes = new RouteCollection(); - $routes->add('acme_privacy', new Route('/privacy', [ - '_controller' => 'Symfony\Bundle\FrameworkBundle\Controller\TemplateController', - 'template' => 'static/privacy.html.twig', - ], [], [], '', [], ['GET'])); + use Symfony\Bundle\FrameworkBundle\Controller\TemplateController; - return $routes; + return function (RoutingConfigurator $routes) { + $routes->add('acme_privacy', '/privacy') + ->controller(TemplateController::class) + ->methods(['GET']) + ->defaults([ + 'template' => 'static/privacy.html.twig', + ]) + ; + }; The ``TemplateController`` will render whatever template you've passed as the ``template`` default value. @@ -114,18 +117,21 @@ exactly how your page is cached: .. code-block:: php // config/routes.php - use Symfony\Component\Routing\RouteCollection; - use Symfony\Component\Routing\Route; - - $routes = new RouteCollection(); - $routes->add('acme_privacy', new Route('/privacy', [ - '_controller' => 'Symfony\Bundle\FrameworkBundle\Controller\TemplateController', - 'template' => 'static/privacy.html.twig', - 'maxAge' => 86400, - 'sharedAge' => 86400, - ], [], [], '', [], ['GET'])); - - return $routes; + namespace Symfony\Component\Routing\Loader\Configurator; + + use Symfony\Bundle\FrameworkBundle\Controller\TemplateController; + + return function (RoutingConfigurator $routes) { + $routes->add('acme_privacy', '/privacy') + ->controller(TemplateController::class) + ->methods(['GET']) + ->defaults([ + 'template' => 'static/privacy.html.twig', + 'maxAge' => 86400, + 'sharedAge' => 86400, + ]) + ; + }; The ``maxAge`` and ``sharedAge`` values are used to modify the Response object created in the controller. For more information on caching, see diff --git a/translation/locale.rst b/translation/locale.rst index 1749ad67e3a..693db124c99 100644 --- a/translation/locale.rst +++ b/translation/locale.rst @@ -91,22 +91,18 @@ by the routing system using the special ``_locale`` parameter: .. code-block:: php // config/routes.php - use Symfony\Component\Routing\RouteCollection; - use Symfony\Component\Routing\Route; + namespace Symfony\Component\Routing\Loader\Configurator; + use App\Controller\ContactController; - $routes = new RouteCollection(); - $routes->add('contact', new Route( - '/{_locale}/contact', - [ - '_controller' => [ContactController::class, 'index'], - ], - [ - '_locale' => 'en|fr|de', - ] - )); - - return $routes; + return function (RoutingConfigurator $routes) { + $routes->add('contact', '/{_locale}/contact') + ->controller([ContactController::class, 'index']) + ->requirements([ + '_locale' => 'en|fr|de', + ]) + ; + }; When using the special ``_locale`` parameter in a route, the matched locale is *automatically set on the Request* and can be retrieved via the