@@ -346,6 +346,181 @@ automatically in the background if you want to use it. A basic example of the
346
346
are saved in the ``cache_dir ``. This means your script must have write
347
347
permissions for that location.
348
348
349
+ Unicode Routing Support
350
+ ~~~~~~~~~~~~~~~~~~~~~~~
351
+
352
+ .. versionadded :: 3.2
353
+ UTF-8 support for route paths and requirements were 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
+ return $collection;
425
+
426
+
427
+ In this route, the ``utf8 `` option set to ``true `` makes Symfony consider the
428
+ ``. `` requirement to match any UTF-8 characters instead of just a single
429
+ byte character. This means that so the following URLs would match:
430
+ ``/category/日本語 ``, ``/category/فارسی ``, ``/category/한국어 ``, etc. In case you
431
+ are wondering, this option also allows to include and match emojis in URLs.
432
+
433
+ .. configuration-block ::
434
+
435
+ .. code-block :: php-annotations
436
+
437
+ // src/AppBundle/Controller/DefaultController.php
438
+ namespace AppBundle\Controller;
439
+
440
+ use Symfony\Bundle\FrameworkBundle\Controller\Controller;
441
+ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
442
+
443
+ class DefaultController extends Controller
444
+ {
445
+ /**
446
+ *
447
+ * @Route(
448
+ * "/category/{name}",
449
+ * name="route2",
450
+ * requirements={"default"="한국어"},
451
+ * options={"utf8": true}
452
+ * )
453
+ *
454
+ */
455
+ public function defaultAction()
456
+ {
457
+ // ...
458
+ }
459
+
460
+ .. code-block :: yaml
461
+
462
+ # routes.yml
463
+ route2 :
464
+ path : /default/{default}
465
+ defaults : { _controller: 'AppBundle:Default:default' }
466
+ requirements :
467
+ default : " 한국어"
468
+ options :
469
+ utf8 : true
470
+
471
+ .. code-block :: xml
472
+
473
+ <!-- app/config/routing.xml -->
474
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
475
+ <routes xmlns =" http://symfony.com/schema/routing"
476
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
477
+ xsi : schemaLocation =" http://symfony.com/schema/routing
478
+ http://symfony.com/schema/routing/routing-1.0.xsd" >
479
+
480
+ <route id =" route2" path =" /default/{default}" >
481
+ <default key =" _controller" >AppBundle:Default:default</default >
482
+ <requirement key =" default" >한국어</requirement >
483
+ <option key =" utf8" >true</option >
484
+ </route >
485
+ </routes >
486
+
487
+ .. code-block :: php
488
+
489
+ // app/config/routing.php
490
+ use Symfony\Component\Routing\RouteCollection;
491
+ use Symfony\Component\Routing\Route;
492
+
493
+ $collection = new RouteCollection();
494
+ $collection->add('route2', new Route('/default/{default}',
495
+ array(
496
+ '_controller' => 'AppBundle:Default:default',
497
+ ),
498
+ array(
499
+ 'default' => '한국어',
500
+ ),
501
+ array(
502
+ 'utf8' => true,
503
+ ));
504
+
505
+ // ...
506
+
507
+ return $collection;
508
+
509
+
510
+ This second example describes how you can use UTF-8 strings as a routing
511
+ requirements.
512
+
513
+ .. note ::
514
+
515
+ In Symfony 3.2 there is no need to set this ``utf8 `` option explicitly.
516
+ As soon as Symfony finds a UTF-8 character in the route path or requirements,
517
+ it will turn the UTF-8 support automatically. In addition to UTF-8
518
+ characters, the Routing component also supports all the `PCRE Unicode properties `_,
519
+ which are escape sequences that match generic character types. For
520
+ example, ``\p{Lu} `` matches any uppercase character in any language,
521
+ ``\p{Greek} `` matches any Greek character, ``\P{Han} `` matches any character
522
+ not included in the Chinese Han script.
523
+
349
524
Learn more
350
525
----------
351
526
@@ -360,3 +535,4 @@ Learn more
360
535
/configuration/apache_router
361
536
362
537
.. _Packagist : https://packagist.org/packages/symfony/routing
538
+ .. _PCRE Unicode properties : http://php.net/manual/en/regexp.reference.unicode.php
0 commit comments