Skip to content

Commit 991b4b6

Browse files
Document new utf8 option of Routing component
1 parent f78d06b commit 991b4b6

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

components/routing.rst

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,44 @@ 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. Thanks to the `utf8` route option, you can make Symfony match and generate routes with UTF-8 characters:
353+
354+
.. code-block:: yaml
355+
356+
# routes.yml
357+
route1:
358+
path: /category/{name}
359+
defaults: { _controller: 'DefaultController::categoryAction' }
360+
requirements:
361+
name: ".+"
362+
options:
363+
utf8: true
364+
365+
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, 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.
366+
367+
.. code-block:: yaml
368+
369+
# routes.yml
370+
route2:
371+
path: /default/{default}
372+
defaults: { _controller: 'DefaultController::defaultAction' }
373+
requirements:
374+
default: "日本語|فارسی"
375+
options:
376+
utf8: true
377+
378+
This second example describes how you can use utf8 string as a routing requirement.
379+
380+
.. note::
381+
382+
In Symfony 3.2 there is no need to set this utf8 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.
383+
384+
.. versionadded:: 3.2
385+
The UTF8 support in route paths and requirements were added in Symfony 3.2
386+
349387
Learn more
350388
----------
351389

@@ -360,3 +398,4 @@ Learn more
360398
/configuration/apache_router
361399

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

0 commit comments

Comments
 (0)