@@ -141,8 +141,11 @@ use them later to :ref:`generate URLs <routing-generate>`.
141
141
Localized Routing (i18n)
142
142
------------------------
143
143
144
- Routes can be localized to provide unique paths per *locale *. Symfony provides a
145
- handy way to declare localized routes without duplication.
144
+ .. versionadded :: 4.1
145
+ The feature to localize routes was introduced in Symfony 4.1.
146
+
147
+ Routes can be localized to provide unique paths per :doc: `locale </translation/locale >`.
148
+ Symfony provides a handy way to declare localized routes without duplication.
146
149
147
150
.. configuration-block ::
148
151
@@ -154,30 +157,18 @@ handy way to declare localized routes without duplication.
154
157
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
155
158
use Symfony\Component\Routing\Annotation\Route;
156
159
157
- class BlogController extends Controller
160
+ class CompanyController extends Controller
158
161
{
159
162
/**
160
- * Matches /blog exactly
161
- *
162
- * @Route("/blog", name="blog_list")
163
+ * @Route({
164
+ * "nl": "/over-ons",
165
+ * "en": "/about-us"
166
+ * }, name="about_us")
163
167
*/
164
- public function list ()
168
+ public function about ()
165
169
{
166
170
// ...
167
171
}
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
172
}
182
173
183
174
.. code-block :: yaml
@@ -214,7 +205,7 @@ handy way to declare localized routes without duplication.
214
205
->controller('App\Controller\CompanyController::about');
215
206
};
216
207
217
- When a localized route is matched Symfony will automatically know which locale
208
+ When a localized route is matched Symfony automatically knows which locale
218
209
should be used during the request. Defining routes this way also eliminated the
219
210
need for duplicate registration of routes which minimizes the risk for any bugs
220
211
caused by definition inconsistency.
@@ -695,16 +686,16 @@ But if you pass extra ones, they will be added to the URI as a query string::
695
686
// /blog/2?category=Symfony
696
687
697
688
Generating Localized URLs
698
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
689
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
699
690
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::
691
+ When a route is localized, Symfony uses by default the current request locale to
692
+ generate the URL . In order to generate the route for a different locale you must
693
+ pass the ``_locale `` in the parameters array::
703
694
704
695
$this->router->generate('about_us', array(
705
696
'_locale' => 'nl',
706
697
));
707
- // /over-ons
698
+ // generates: /over-ons
708
699
709
700
Generating URLs from a Template
710
701
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0 commit comments