Skip to content

Commit 87be2eb

Browse files
committed
Initial docs on i18n routing.
1 parent f9e4f9d commit 87be2eb

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

routing.rst

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,89 @@ use them later to :ref:`generate URLs <routing-generate>`.
136136
configure your routes in YAML, XML or PHP, that's no problem! Just create a
137137
new routing file (e.g. ``routes.xml``) and Symfony will automatically use it.
138138

139+
.. _i18n-routing:
140+
141+
Localized Routing (i18n)
142+
------------------------
143+
144+
Routes can be localized to provide unique paths per *locale*. Symfony provides a
145+
handle way to declare localized routes without duplication.
146+
147+
.. configuration-block::
148+
149+
.. code-block:: php-annotations
150+
151+
// src/Controller/BlogController.php
152+
namespace App\Controller;
153+
154+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
155+
use Symfony\Component\Routing\Annotation\Route;
156+
157+
class BlogController extends Controller
158+
{
159+
/**
160+
* Matches /blog exactly
161+
*
162+
* @Route("/blog", name="blog_list")
163+
*/
164+
public function list()
165+
{
166+
// ...
167+
}
168+
169+
/**
170+
* Matches /blog/*
171+
*
172+
* @Route("/blog/{slug}", name="blog_show")
173+
*/
174+
public function show($slug)
175+
{
176+
// $slug will equal the dynamic part of the URL
177+
// e.g. at /blog/yay-routing, then $slug='yay-routing'
178+
179+
// ...
180+
}
181+
}
182+
183+
.. code-block:: yaml
184+
185+
# config/routes.yaml
186+
about_us:
187+
path:
188+
nl: /over-ons
189+
en: /about-us
190+
controller: App\Controller\CompanyController::about
191+
192+
.. code-block:: xml
193+
194+
<!-- config/routes.xml -->
195+
<?xml version="1.0" encoding="UTF-8" ?>
196+
<routes xmlns="http://symfony.com/schema/routing"
197+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
198+
xsi:schemaLocation="http://symfony.com/schema/routing
199+
http://symfony.com/schema/routing/routing-1.0.xsd">
200+
201+
<route id="about_us" controller="App\Controller\CompanyController::about">
202+
<path locale="nl">/over-ons</path>
203+
<path locale="en">/about-us</path>
204+
</route>
205+
</routes>
206+
207+
.. code-block:: php
208+
209+
// config/routes.php
210+
namespace Symfony\Component\Routing\Loader\Configurator;
211+
212+
return function (RoutingConfigurator $routes) {
213+
$routes->add('about_us', ['nl' => '/over-ons', 'en' => '/about-us'])
214+
->controller('App\Controller\CompanyController::about');
215+
};
216+
217+
When a localized route is matched Symfony will automatically know which locale
218+
should be used during the request. Defining routes this way also eliminated the
219+
need for duplicate registration of routes which minimizes the risk for any bugs
220+
caused by definition inconsistency.
221+
139222
.. _routing-requirements:
140223

141224
Adding {wildcard} Requirements
@@ -611,6 +694,18 @@ But if you pass extra ones, they will be added to the URI as a query string::
611694
));
612695
// /blog/2?category=Symfony
613696

697+
Generating Localized URLs
698+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
699+
700+
When you've defined localized routes Symfony will use the current request locale
701+
as the default when generating routes. In order to generate the route for alternative
702+
locale you must pass the ``_locale`` in the parameters array::
703+
704+
$this->router->generate('about_us', array(
705+
'_locale' => 'nl',
706+
));
707+
// /over-ons
708+
614709
Generating URLs from a Template
615710
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
616711

0 commit comments

Comments
 (0)