Skip to content

[Routing][DX] Exposed "utf8" options, and defaults "locale" and "format" #11126

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 3 commits into from
Mar 25, 2019
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
82 changes: 65 additions & 17 deletions components/routing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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

Expand All @@ -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">

<route id="route1" path="/category/{name}" controller="App\Controller\DefaultController::category">
<option key="utf8">true</option>
</route>
<route id="route1"
path="/category/{name}"
controller="App\Controller\DefaultController::category"
utf8="true" />
</routes>

.. code-block:: php
Expand All @@ -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

<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
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">

<route id="route1"
path="/category/{name}"
controller="App\Controller\DefaultController::category" >
<option key="utf8">true</option>
</route>
</routes>

.. 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:
Expand All @@ -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()
Expand All @@ -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

Expand All @@ -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">

<route id="route2" path="/category/{name}" controller="App\Controller\DefaultController::category">
<route id="route2"
path="/category/{name}"
controller="App\Controller\DefaultController::category"
utf8="true" >
<default key="name">한국어</default>
<option key="utf8">true</option>
</route>
</routes>

Expand All @@ -512,9 +562,7 @@ You can also include UTF-8 strings as routing requirements:
->defaults([
'name' => '한국어',
])
->options([
'utf8' => true,
])
->utf8()
;
};

Expand Down
93 changes: 92 additions & 1 deletion routing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
Expand All @@ -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">

<default key="_locale">en</default>
<default key="_format">html</default>
<requirement key="_locale">en|fr</requirement>
<requirement key="_format">html|rss</requirement>
Expand All @@ -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([
Expand Down Expand Up @@ -739,6 +745,91 @@ 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 <translation-locale-url>`).

You can also use special attributes to configure them (except ``_fragment``):

.. configuration-block::

.. code-block:: php-annotations

// src/Controller/ArticleController.php

// ...
class ArticleController extends AbstractController
{
/**
* @Route(
* "/articles/{_locale}/search.{_format}",
* locale="en",
* format="html",
* 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

<!-- config/routes.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
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">

<route id="article_search"
path="/articles/{_locale}/search.{_format}"
controller="App\Controller\ArticleController::search"
locale="en"
format="html">

<requirement key="_locale">en|fr</requirement>
<requirement key="_format">html|rss</requirement>

</route>
</routes>

.. 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',
])
;
};

Those attributes can also be used for imports.

.. versionadded::

The special attributes has been introduced in Symfony 4.3.

.. _routing-trailing-slash-redirection:

Redirecting URLs with Trailing Slashes
Expand Down