From 06a71a9a33174ec5fd840b811f87263dab8bc213 Mon Sep 17 00:00:00 2001 From: Jules Pietri Date: Sun, 10 Mar 2019 19:33:48 +0100 Subject: [PATCH 1/3] [Routing][DX] Exposed "compiler_class" and "utf8" options --- components/routing.rst | 82 +++++++++++++++++++++++++++++++++--------- 1 file changed, 65 insertions(+), 17 deletions(-) diff --git a/components/routing.rst b/components/routing.rst index a46922505ea..9c9c454cd3b 100644 --- a/components/routing.rst +++ b/components/routing.rst @@ -399,7 +399,7 @@ routes with UTF-8 characters: class DefaultController extends AbstractController { /** - * @Route("/category/{name}", name="route1", options={"utf8": true}) + * @Route("/category/{name}", name="route1", utf8=true) */ public function category() { @@ -411,8 +411,7 @@ routes with UTF-8 characters: route1: path: /category/{name} controller: App\Controller\DefaultController::category - options: - utf8: true + utf8: true .. code-block:: xml @@ -422,9 +421,10 @@ routes with UTF-8 characters: xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - - - + .. code-block:: php @@ -437,12 +437,61 @@ routes with UTF-8 characters: return function (RoutingConfigurator $routes) { $routes->add('route1', '/category/{name}') ->controller([DefaultController::class, 'category']) - ->options([ - 'utf8' => true, - ]) + ->utf8() ; }; +.. versionadded:: 4.3 + + The ``utf8`` shortcut has been introduced in Symfony 4.3. + Before you has to use the ``options`` setting to define + this value: + + .. configuration-block:: + + .. code-block:: php-annotations + + route1: + path: /category/{name} + controller: App\Controller\DefaultController::category + options: { utf8: true } + + .. code-block:: yaml + + route1: + path: /category/{name} + controller: App\Controller\DefaultController::category + utf8: true + + .. code-block:: xml + + + + + + + + + + .. code-block:: php + + // config/routes.php + namespace Symfony\Component\Routing\Loader\Configurator; + + use App\Controller\DefaultController; + + 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 byte character. This means that so the following URLs would match: @@ -467,7 +516,7 @@ You can also include UTF-8 strings as routing requirements: * "/category/{name}", * name="route2", * defaults={"name"="한국어"}, - * options={"utf8": true} + * utf8=true * ) */ public function category() @@ -482,8 +531,7 @@ You can also include UTF-8 strings as routing requirements: controller: 'App\Controller\DefaultController::category' defaults: name: "한국어" - options: - utf8: true + utf8: true .. code-block:: xml @@ -493,9 +541,11 @@ 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"> - + 한국어 - @@ -512,9 +562,7 @@ You can also include UTF-8 strings as routing requirements: ->defaults([ 'name' => '한국어', ]) - ->options([ - 'utf8' => true, - ]) + ->utf8() ; }; From f1eae4496e588674384d6e3ca98df390ec69e778 Mon Sep 17 00:00:00 2001 From: Jules Pietri Date: Sun, 17 Mar 2019 18:33:56 +0100 Subject: [PATCH 2/3] added doc for defaults --- routing.rst | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 93 insertions(+), 1 deletion(-) diff --git a/routing.rst b/routing.rst index 10e708d829e..553f29b5487 100644 --- a/routing.rst +++ b/routing.rst @@ -623,7 +623,10 @@ With all of this in mind, check out this advanced example: /** * @Route( * "/articles/{_locale}/{year}/{slug}.{_format}", - * defaults={"_format": "html"}, + * defaults={ + * "_locale": "en", + * "_format": "html" + * }, * requirements={ * "_locale": "en|fr", * "_format": "html|rss", @@ -643,6 +646,7 @@ With all of this in mind, check out this advanced example: path: /articles/{_locale}/{year}/{slug}.{_format} controller: App\Controller\ArticleController::show defaults: + _locale: en _format: html requirements: _locale: en|fr @@ -662,6 +666,7 @@ With all of this in mind, check out this advanced example: path="/articles/{_locale}/{year}/{slug}.{_format}" controller="App\Controller\ArticleController::show"> + en html en|fr html|rss @@ -681,6 +686,7 @@ With all of this in mind, check out this advanced example: $routes->add('article_show', '/articles/{_locale}/{year}/{slug}.{_format}') ->controller([ArticleController::class, 'show']) ->defaults([ + '_locale' => 'en', '_format' => 'html', ]) ->requirements([ @@ -739,6 +745,92 @@ that are special: each adds a unique piece of functionality inside your applicat ``_locale`` Used to set the locale on the request (:ref:`read more `). +You can also use special attributes to configure them: + +.. configuration-block:: + + .. code-block:: php-annotations + + // src/Controller/ArticleController.php + + // ... + class ArticleController extends AbstractController + { + /** + * @Route( + * "/articles/{_locale}/search.{_format}", + * locale="en", + * format="html", + * fragment="first-result-id", + * requirements={ + * "_locale": "en|fr", + * "_format": "html|xml", + * } + * ) + */ + public function search() + { + } + } + + .. code-block:: yaml + + # config/routes.yaml + article_search: + path: /articles/{_locale}/search.{_format} + controller: App\Controller\ArticleController::search + locale: en + format: html + requirements: + _locale: en|fr + _format: html|xml + + .. code-block:: xml + + + + + + + + en|fr + html|rss + \d+ + + + + + .. code-block:: php + + // config/routes.php + namespace Symfony\Component\Routing\Loader\Configurator; + + use App\Controller\ArticleController; + + return function (RoutingConfigurator $routes) { + $routes->add('article_show', '/articles/{_locale}/search.{_format}') + ->controller([ArticleController::class, 'search']) + ->locale('en') + ->format('html) + ->requirements([ + '_locale' => 'en|fr', + '_format' => 'html|rss', + 'year' => '\d+', + ]) + ; + }; + +.. versionadded:: + + The special attributes has been introduced in Symfony 4.3. + .. _routing-trailing-slash-redirection: Redirecting URLs with Trailing Slashes From f9e2eaa1d1de665e452d417728fcd3d85787e4d4 Mon Sep 17 00:00:00 2001 From: Jules Pietri Date: Sun, 17 Mar 2019 19:08:49 +0100 Subject: [PATCH 3/3] fixup --- routing.rst | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/routing.rst b/routing.rst index 553f29b5487..357fc5f36a6 100644 --- a/routing.rst +++ b/routing.rst @@ -745,7 +745,7 @@ that are special: each adds a unique piece of functionality inside your applicat ``_locale`` Used to set the locale on the request (:ref:`read more `). -You can also use special attributes to configure them: +You can also use special attributes to configure them (except ``_fragment``): .. configuration-block:: @@ -761,7 +761,6 @@ You can also use special attributes to configure them: * "/articles/{_locale}/search.{_format}", * locale="en", * format="html", - * fragment="first-result-id", * requirements={ * "_locale": "en|fr", * "_format": "html|xml", @@ -802,7 +801,6 @@ You can also use special attributes to configure them: en|fr html|rss - \d+ @@ -822,11 +820,12 @@ You can also use special attributes to configure them: ->requirements([ '_locale' => 'en|fr', '_format' => 'html|rss', - 'year' => '\d+', ]) ; }; +Those attributes can also be used for imports. + .. versionadded:: The special attributes has been introduced in Symfony 4.3.