Skip to content

Commit 4d1b926

Browse files
committed
feature #6938 Document new utf8 option of Routing component (mickaelandrieu)
This PR was merged into the master branch. Discussion ---------- Document new utf8 option of Routing component Hi, all credits to @javiereguiluz and @nicolas-grekas, I've just copy/pasted the blog article http://symfony.com/blog/new-in-symfony-3-2-unicode-routing-support#comment-form Mickaël Commits ------- 2569aca Document new utf8 option of Routing component
2 parents 2f866f2 + 2569aca commit 4d1b926

File tree

1 file changed

+178
-0
lines changed

1 file changed

+178
-0
lines changed

components/routing.rst

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,183 @@ 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+
# routes.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+
.. configuration-block::
435+
436+
.. code-block:: php-annotations
437+
438+
// src/AppBundle/Controller/DefaultController.php
439+
namespace AppBundle\Controller;
440+
441+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
442+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
443+
444+
class DefaultController extends Controller
445+
{
446+
/**
447+
*
448+
* @Route(
449+
* "/category/{name}",
450+
* name="route2",
451+
* requirements={"default"="한국어"},
452+
* options={"utf8": true}
453+
* )
454+
*
455+
*/
456+
public function defaultAction()
457+
{
458+
// ...
459+
}
460+
461+
.. code-block:: yaml
462+
463+
# routes.yml
464+
route2:
465+
path: /default/{default}
466+
defaults: { _controller: 'AppBundle:Default:default' }
467+
requirements:
468+
default: "한국어"
469+
options:
470+
utf8: true
471+
472+
.. code-block:: xml
473+
474+
<!-- app/config/routing.xml -->
475+
<?xml version="1.0" encoding="UTF-8" ?>
476+
<routes xmlns="http://symfony.com/schema/routing"
477+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
478+
xsi:schemaLocation="http://symfony.com/schema/routing
479+
http://symfony.com/schema/routing/routing-1.0.xsd">
480+
481+
<route id="route2" path="/default/{default}">
482+
<default key="_controller">AppBundle:Default:default</default>
483+
<requirement key="default">한국어</requirement>
484+
<option key="utf8">true</option>
485+
</route>
486+
</routes>
487+
488+
.. code-block:: php
489+
490+
// app/config/routing.php
491+
use Symfony\Component\Routing\RouteCollection;
492+
use Symfony\Component\Routing\Route;
493+
494+
$collection = new RouteCollection();
495+
$collection->add('route2', new Route('/default/{default}',
496+
array(
497+
'_controller' => 'AppBundle:Default:default',
498+
),
499+
array(
500+
'default' => '한국어',
501+
),
502+
array(
503+
'utf8' => true,
504+
)
505+
);
506+
507+
// ...
508+
509+
return $collection;
510+
511+
512+
This second example describes how you can use UTF-8 strings as a routing
513+
requirements.
514+
515+
.. note::
516+
517+
In Symfony 3.2 there is no need to set this ``utf8`` option explicitly.
518+
As soon as Symfony finds a UTF-8 character in the route path or requirements,
519+
it will turn the UTF-8 support automatically. In addition to UTF-8
520+
characters, the Routing component also supports all the `PCRE Unicode properties`_,
521+
which are escape sequences that match generic character types. For
522+
example, ``\p{Lu}`` matches any uppercase character in any language,
523+
``\p{Greek}`` matches any Greek character, ``\P{Han}`` matches any character
524+
not included in the Chinese Han script.
525+
349526
Learn more
350527
----------
351528

@@ -360,3 +537,4 @@ Learn more
360537
/configuration/apache_router
361538

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

0 commit comments

Comments
 (0)