-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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
Document new utf8 option of Routing component #6938
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -346,6 +346,183 @@ automatically in the background if you want to use it. A basic example of the | |
are saved in the ``cache_dir``. This means your script must have write | ||
permissions for that location. | ||
|
||
Unicode Routing Support | ||
~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
.. versionadded:: 3.2 | ||
UTF-8 support for route paths and requirements was introduced in | ||
Symfony 3.2. | ||
|
||
The Routing component supports UTF-8 characters in route paths and requirements. | ||
Thanks to the ``utf8`` route option, you can make Symfony match and generate | ||
routes with UTF-8 characters: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: php-annotations | ||
|
||
// src/AppBundle/Controller/DefaultController.php | ||
namespace AppBundle\Controller; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | ||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | ||
|
||
class DefaultController extends Controller | ||
{ | ||
/** | ||
* | ||
* @Route("/category/{name}", name="route1", options={"utf8": true}) | ||
* | ||
*/ | ||
public function categoryAction() | ||
{ | ||
// ... | ||
} | ||
|
||
.. code-block:: yaml | ||
|
||
# routes.yml | ||
route1: | ||
path: /category/{name} | ||
defaults: { _controller: 'AppBundle:Default:category' } | ||
options: | ||
utf8: true | ||
|
||
.. code-block:: xml | ||
|
||
<!-- app/config/routing.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}"> | ||
<default key="_controller">AppBundle:Default:category</default> | ||
<option key="utf8">true</option> | ||
</route> | ||
</routes> | ||
|
||
.. code-block:: php | ||
|
||
// app/config/routing.php | ||
use Symfony\Component\Routing\RouteCollection; | ||
use Symfony\Component\Routing\Route; | ||
|
||
$collection = new RouteCollection(); | ||
$collection->add('route1', new Route('/category/{name}', | ||
array( | ||
'_controller' => 'AppBundle:Default:category', | ||
), | ||
array(), | ||
array( | ||
'utf8' => true, | ||
) | ||
); | ||
|
||
// ... | ||
|
||
return $collection; | ||
|
||
|
||
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: | ||
``/category/日本語``, ``/category/فارسی``, ``/category/한국어``, etc. In case you | ||
are wondering, this option also allows to include and match emojis in URLs. | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: php-annotations | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please indent with four spaces |
||
|
||
// src/AppBundle/Controller/DefaultController.php | ||
namespace AppBundle\Controller; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | ||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | ||
|
||
class DefaultController extends Controller | ||
{ | ||
/** | ||
* | ||
* @Route( | ||
* "/category/{name}", | ||
* name="route2", | ||
* requirements={"default"="한국어"}, | ||
* options={"utf8": true} | ||
* ) | ||
* | ||
*/ | ||
public function defaultAction() | ||
{ | ||
// ... | ||
} | ||
|
||
.. code-block:: yaml | ||
|
||
# routes.yml | ||
route2: | ||
path: /default/{default} | ||
defaults: { _controller: 'AppBundle:Default:default' } | ||
requirements: | ||
default: "한국어" | ||
options: | ||
utf8: true | ||
|
||
.. code-block:: xml | ||
|
||
<!-- app/config/routing.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="route2" path="/default/{default}"> | ||
<default key="_controller">AppBundle:Default:default</default> | ||
<requirement key="default">한국어</requirement> | ||
<option key="utf8">true</option> | ||
</route> | ||
</routes> | ||
|
||
.. code-block:: php | ||
|
||
// app/config/routing.php | ||
use Symfony\Component\Routing\RouteCollection; | ||
use Symfony\Component\Routing\Route; | ||
|
||
$collection = new RouteCollection(); | ||
$collection->add('route2', new Route('/default/{default}', | ||
array( | ||
'_controller' => 'AppBundle:Default:default', | ||
), | ||
array( | ||
'default' => '한국어', | ||
), | ||
array( | ||
'utf8' => true, | ||
) | ||
); | ||
|
||
// ... | ||
|
||
return $collection; | ||
|
||
|
||
This second example describes how you can use UTF-8 strings as a routing | ||
requirements. | ||
|
||
.. note:: | ||
|
||
In Symfony 3.2 there is no need to set this ``utf8`` option explicitly. | ||
As soon as Symfony finds a UTF-8 character in the route path or requirements, | ||
it will turn the UTF-8 support automatically. In addition to UTF-8 | ||
characters, the Routing component also supports all the `PCRE Unicode properties`_, | ||
which are escape sequences that match generic character types. For | ||
example, ``\p{Lu}`` matches any uppercase character in any language, | ||
``\p{Greek}`` matches any Greek character, ``\P{Han}`` matches any character | ||
not included in the Chinese Han script. | ||
|
||
Learn more | ||
---------- | ||
|
||
|
@@ -360,3 +537,4 @@ Learn more | |
/configuration/apache_router | ||
|
||
.. _Packagist: https://packagist.org/packages/symfony/routing | ||
.. _PCRE Unicode properties: http://php.net/manual/en/regexp.reference.unicode.php |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question here: how can we do to ensure true will be converted to boolean value ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's the responsibility of the loader.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So ... is it good, or does it need much work from me ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's good as it is. :)