Skip to content
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 1 commit into from
Oct 6, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 178 additions & 0 deletions components/routing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Copy link
Contributor Author

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 ?

Copy link
Member

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.

Copy link
Contributor Author

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 ?

Copy link
Member

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. :)

</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
Copy link
Member

Choose a reason for hiding this comment

The 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
----------

Expand All @@ -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