Skip to content

Commit cf8ab0f

Browse files
Document new utf8 option of Routing component
1 parent 09975d7 commit cf8ab0f

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

components/routing.rst

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,59 @@ automatically in the background if you want to use it. A basic example of the
346346
are saved in the ``cache_dir``. This means your script must have write
347347
permissions for that location.
348348

349+
Unicode Routing support
350+
~~~~~~~~~~~~~~~~~~~~~~~
351+
352+
The Routing component supports UTF-8 characters in route paths and requirements.
353+
Thanks to the ``utf8`` route option, you can make Symfony match and generate
354+
routes with UTF-8 characters:
355+
356+
.. code-block:: yaml
357+
358+
# routes.yml
359+
route1:
360+
path: /category/{name}
361+
defaults: { _controller: 'DefaultController::categoryAction' }
362+
requirements:
363+
name: ".+"
364+
options:
365+
utf8: true
366+
367+
In this route, the ``utf8`` option set to true makes Symfony consider the
368+
``.`` requirement to match any UTF-8 characters instead of just a single
369+
byte character, so the following URLs would match: ``/category/日本語``,
370+
``/category/فارسی``, ``/category/한국어``, etc. In case you are wondering,
371+
this option also allows to include and match emojis in URLs.
372+
373+
.. code-block:: yaml
374+
375+
# routes.yml
376+
route2:
377+
path: /default/{default}
378+
defaults: { _controller: 'DefaultController::defaultAction' }
379+
requirements:
380+
default: "日本語|فارسی"
381+
options:
382+
utf8: true
383+
384+
This second example describes how you can use UTF-8 string as a routing
385+
requirement.
386+
387+
.. note::
388+
389+
In Symfony 3.2 there is no need to set this utf8 explicitly. As soon
390+
as Symfony finds a UTF-8 character in the route path or requirements,
391+
it will turn the UTF-8 support automatically. In addition to UTF-8
392+
characters, the Routing component also supports all the `PCRE Unicode properties`_,
393+
which are escape sequences that match generic character types. For
394+
example, ``\p{Lu}`` matches any uppercase character in any language,
395+
``\p{Greek}`` matches any Greek character, ``\P{Han}`` matches any character
396+
not included in the Chinese Han script.
397+
398+
.. versionadded:: 3.2
399+
UTF-8 support for route paths and requirements were introduced in
400+
Symfony 3.2.
401+
349402
Learn more
350403
----------
351404

@@ -360,3 +413,4 @@ Learn more
360413
/configuration/apache_router
361414

362415
.. _Packagist: https://packagist.org/packages/symfony/routing
416+
.. _PCRE Unicode properties: http://php.net/manual/en/regexp.reference.unicode.php

0 commit comments

Comments
 (0)