@@ -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
+ The Routing component supports UTF-8 characters in route paths and requirements.
353
+ Thanks to the ``utf8 `` route option, you can make Symfony match and generate
354
+ routes with UTF-8 characters:
355
+
356
+ .. configuration-block ::
357
+
358
+ .. code-block :: php-annotations
359
+
360
+ // src/AppBundle/Controller/DefaultController.php
361
+ namespace AppBundle\Controller;
362
+
363
+ use Symfony\Bundle\FrameworkBundle\Controller\Controller;
364
+ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
365
+
366
+ class DefaultController extends Controller
367
+ {
368
+ /**
369
+ *
370
+ * @Route("/category/{name}", name="route1", options={"utf8": true})
371
+ *
372
+ */
373
+ public function categoryAction()
374
+ {
375
+ // ...
376
+ }
377
+
378
+ .. code-block :: yaml
379
+
380
+ # routes.yml
381
+ route1 :
382
+ path : /category/{name}
383
+ defaults : { _controller: 'DefaultController::categoryAction' }
384
+ options :
385
+ utf8 : true
386
+
387
+ .. code-block :: xml
388
+
389
+ <!-- app/config/routing.xml -->
390
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
391
+ <routes xmlns =" http://symfony.com/schema/routing"
392
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
393
+ xsi : schemaLocation =" http://symfony.com/schema/routing
394
+ http://symfony.com/schema/routing/routing-1.0.xsd" >
395
+
396
+ <route id =" route1" path =" /category/{name}" >
397
+ <default key =" _controller" >AppBundle:Default:category</default >
398
+ <option key =" utf8" >true</option >
399
+ </route >
400
+ </routes >
401
+
402
+ .. code-block :: php
403
+
404
+ // app/config/routing.php
405
+ use Symfony\Component\Routing\RouteCollection;
406
+ use Symfony\Component\Routing\Route;
407
+
408
+ $collection = new RouteCollection();
409
+ $collection->add('route1', new Route('/category/{name}',
410
+ array(
411
+ '_controller' => 'AppBundle:Default:category',
412
+ ),
413
+ array(),
414
+ array(
415
+ 'utf8' => true,
416
+ ));
417
+
418
+ // ...
419
+
420
+ return $collection;
421
+
422
+
423
+ In this route, the ``utf8 `` option set to true makes Symfony consider the
424
+ ``. `` requirement to match any UTF-8 characters instead of just a single
425
+ byte character, so the following URLs would match: ``/category/日本語 ``,
426
+ ``/category/فارسی ``, ``/category/한국어 ``, etc. In case you are wondering,
427
+ this option also allows to include and match emojis in URLs.
428
+
429
+ .. configuration-block ::
430
+
431
+ .. code-block :: php-annotations
432
+
433
+ // src/AppBundle/Controller/DefaultController.php
434
+ namespace AppBundle\Controller;
435
+
436
+ use Symfony\Bundle\FrameworkBundle\Controller\Controller;
437
+ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
438
+
439
+ class DefaultController extends Controller
440
+ {
441
+ /**
442
+ *
443
+ * @Route(
444
+ * "/category/{name}",
445
+ * name="route2",
446
+ * requirements={"default"="한국어"},
447
+ * options={"utf8": true}
448
+ * )
449
+ *
450
+ */
451
+ public function defaultAction()
452
+ {
453
+ // ...
454
+ }
455
+
456
+ .. code-block :: yaml
457
+
458
+ # routes.yml
459
+ route2 :
460
+ path : /default/{default}
461
+ defaults : { _controller: 'DefaultController::defaultAction' }
462
+ requirements :
463
+ default : " 한국어"
464
+ options :
465
+ utf8 : true
466
+
467
+ .. code-block :: xml
468
+
469
+ <!-- app/config/routing.xml -->
470
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
471
+ <routes xmlns =" http://symfony.com/schema/routing"
472
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
473
+ xsi : schemaLocation =" http://symfony.com/schema/routing
474
+ http://symfony.com/schema/routing/routing-1.0.xsd" >
475
+
476
+ <route id =" route2" path =" /default/{default}" >
477
+ <default key =" _controller" >AppBundle:Default:default</default >
478
+ <requirement key =" default" >한국어</requirement >
479
+ <option key =" utf8" >true</option >
480
+ </route >
481
+ </routes >
482
+
483
+ .. code-block :: php
484
+
485
+ // app/config/routing.php
486
+ use Symfony\Component\Routing\RouteCollection;
487
+ use Symfony\Component\Routing\Route;
488
+
489
+ $collection = new RouteCollection();
490
+ $collection->add('route2', new Route('/default/{default}',
491
+ array(
492
+ '_controller' => 'AppBundle:Default:default',
493
+ ),
494
+ array(
495
+ 'default' => '한국어',
496
+ ),
497
+ array(
498
+ 'utf8' => true,
499
+ ));
500
+
501
+ // ...
502
+
503
+ return $collection;
504
+
505
+
506
+ This second example describes how you can use UTF-8 string as a routing
507
+ requirement.
508
+
509
+ .. note ::
510
+
511
+ In Symfony 3.2 there is no need to set this utf8 explicitly. As soon
512
+ as Symfony finds a UTF-8 character in the route path or requirements,
513
+ it will turn the UTF-8 support automatically. In addition to UTF-8
514
+ characters, the Routing component also supports all the `PCRE Unicode properties `_,
515
+ which are escape sequences that match generic character types. For
516
+ example, ``\p{Lu} `` matches any uppercase character in any language,
517
+ ``\p{Greek} `` matches any Greek character, ``\P{Han} `` matches any character
518
+ not included in the Chinese Han script.
519
+
520
+ .. versionadded :: 3.2
521
+ UTF-8 support for route paths and requirements were introduced in
522
+ Symfony 3.2.
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