@@ -346,6 +346,185 @@ 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+ .. versionadded :: 3.2
353+ UTF-8 support for route paths and requirements was introduced in
354+ Symfony 3.2.
355+
356+ The Routing component supports UTF-8 characters in route paths and requirements.
357+ Thanks to the ``utf8 `` route option, you can make Symfony match and generate
358+ routes with UTF-8 characters:
359+
360+ .. configuration-block ::
361+
362+ .. code-block :: php-annotations
363+
364+ // src/AppBundle/Controller/DefaultController.php
365+ namespace AppBundle\Controller;
366+
367+ use Symfony\Bundle\FrameworkBundle\Controller\Controller;
368+ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
369+
370+ class DefaultController extends Controller
371+ {
372+ /**
373+ *
374+ * @Route("/category/{name}", name="route1", options={"utf8": true})
375+ *
376+ */
377+ public function categoryAction()
378+ {
379+ // ...
380+ }
381+
382+ .. code-block :: yaml
383+
384+ # app/config/routing.yml
385+ route1 :
386+ path : /category/{name}
387+ defaults : { _controller: 'AppBundle:Default:category' }
388+ options :
389+ utf8 : true
390+
391+ .. code-block :: xml
392+
393+ <!-- app/config/routing.xml -->
394+ <?xml version =" 1.0" encoding =" UTF-8" ?>
395+ <routes xmlns =" http://symfony.com/schema/routing"
396+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
397+ xsi : schemaLocation =" http://symfony.com/schema/routing
398+ http://symfony.com/schema/routing/routing-1.0.xsd" >
399+
400+ <route id =" route1" path =" /category/{name}" >
401+ <default key =" _controller" >AppBundle:Default:category</default >
402+ <option key =" utf8" >true</option >
403+ </route >
404+ </routes >
405+
406+ .. code-block :: php
407+
408+ // app/config/routing.php
409+ use Symfony\Component\Routing\RouteCollection;
410+ use Symfony\Component\Routing\Route;
411+
412+ $collection = new RouteCollection();
413+ $collection->add('route1', new Route('/category/{name}',
414+ array(
415+ '_controller' => 'AppBundle:Default:category',
416+ ),
417+ array(),
418+ array(
419+ 'utf8' => true,
420+ )
421+ );
422+
423+ // ...
424+
425+ return $collection;
426+
427+
428+ In this route, the ``utf8 `` option set to ``true `` makes Symfony consider the
429+ ``. `` requirement to match any UTF-8 characters instead of just a single
430+ byte character. This means that so the following URLs would match:
431+ ``/category/日本語 ``, ``/category/فارسی ``, ``/category/한국어 ``, etc. In case you
432+ are wondering, this option also allows to include and match emojis in URLs.
433+
434+ You can also include UTF-8 strings as routing requirements:
435+
436+ .. configuration-block ::
437+
438+ .. code-block :: php-annotations
439+
440+ // src/AppBundle/Controller/DefaultController.php
441+ namespace AppBundle\Controller;
442+
443+ use Symfony\Bundle\FrameworkBundle\Controller\Controller;
444+ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
445+
446+ class DefaultController extends Controller
447+ {
448+ /**
449+ *
450+ * @Route(
451+ * "/category/{name}",
452+ * name="route2",
453+ * requirements={"default"="한국어"},
454+ * options={"utf8": true}
455+ * )
456+ *
457+ */
458+ public function defaultAction()
459+ {
460+ // ...
461+ }
462+
463+ .. code-block :: yaml
464+
465+ # app/config/routing.yml
466+ route2 :
467+ path : /default/{default}
468+ defaults : { _controller: 'AppBundle:Default:default' }
469+ requirements :
470+ default : " 한국어"
471+ options :
472+ utf8 : true
473+
474+ .. code-block :: xml
475+
476+ <!-- app/config/routing.xml -->
477+ <?xml version =" 1.0" encoding =" UTF-8" ?>
478+ <routes xmlns =" http://symfony.com/schema/routing"
479+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
480+ xsi : schemaLocation =" http://symfony.com/schema/routing
481+ http://symfony.com/schema/routing/routing-1.0.xsd" >
482+
483+ <route id =" route2" path =" /default/{default}" >
484+ <default key =" _controller" >AppBundle:Default:default</default >
485+ <requirement key =" default" >한국어</requirement >
486+ <option key =" utf8" >true</option >
487+ </route >
488+ </routes >
489+
490+ .. code-block :: php
491+
492+ // app/config/routing.php
493+ use Symfony\Component\Routing\RouteCollection;
494+ use Symfony\Component\Routing\Route;
495+
496+ $collection = new RouteCollection();
497+ $collection->add('route2', new Route('/default/{default}',
498+ array(
499+ '_controller' => 'AppBundle:Default:default',
500+ ),
501+ array(
502+ 'default' => '한국어',
503+ ),
504+ array(
505+ 'utf8' => true,
506+ )
507+ );
508+
509+ // ...
510+
511+ return $collection;
512+
513+ .. tip ::
514+
515+ In addition to UTF-8 characters, the Routing component also supports all
516+ the `PCRE Unicode properties `_, which are escape sequences that match
517+ generic character types. For example, ``\p{Lu} `` matches any uppercase
518+ character in any language, ``\p{Greek} `` matches any Greek character,
519+ ``\P{Han} `` matches any character not included in the Chinese Han script.
520+
521+ .. note ::
522+
523+ In Symfony 3.2 there is no need to set the ``utf8 `` option explicitly. As
524+ soon as Symfony finds a UTF-8 character in the route path or requirements,
525+ it will turn the UTF-8 support automatically. However, this behaviour is
526+ deprecated and setting the option will be required in Symfony 4.0.
527+
349528Learn more
350529----------
351530
@@ -360,3 +539,4 @@ Learn more
360539 /configuration/apache_router
361540
362541.. _Packagist : https://packagist.org/packages/symfony/routing
542+ .. _PCRE Unicode properties : http://php.net/manual/en/regexp.reference.unicode.php
0 commit comments