@@ -623,7 +623,10 @@ With all of this in mind, check out this advanced example:
623
623
/**
624
624
* @Route(
625
625
* "/articles/{_locale}/{year}/{slug}.{_format}",
626
- * defaults={"_format": "html"},
626
+ * defaults={
627
+ * "_locale": "en",
628
+ * "_format": "html"
629
+ * },
627
630
* requirements={
628
631
* "_locale": "en|fr",
629
632
* "_format": "html|rss",
@@ -643,6 +646,7 @@ With all of this in mind, check out this advanced example:
643
646
path : /articles/{_locale}/{year}/{slug}.{_format}
644
647
controller : App\Controller\ArticleController::show
645
648
defaults :
649
+ _locale : en
646
650
_format : html
647
651
requirements :
648
652
_locale : en|fr
@@ -662,6 +666,7 @@ With all of this in mind, check out this advanced example:
662
666
path =" /articles/{_locale}/{year}/{slug}.{_format}"
663
667
controller =" App\Controller\ArticleController::show" >
664
668
669
+ <default key =" _locale" >en</default >
665
670
<default key =" _format" >html</default >
666
671
<requirement key =" _locale" >en|fr</requirement >
667
672
<requirement key =" _format" >html|rss</requirement >
@@ -681,6 +686,7 @@ With all of this in mind, check out this advanced example:
681
686
$routes->add('article_show', '/articles/{_locale}/{year}/{slug}.{_format}')
682
687
->controller([ArticleController::class, 'show'])
683
688
->defaults([
689
+ '_locale' => 'en',
684
690
'_format' => 'html',
685
691
])
686
692
->requirements([
@@ -739,6 +745,92 @@ that are special: each adds a unique piece of functionality inside your applicat
739
745
``_locale ``
740
746
Used to set the locale on the request (:ref: `read more <translation-locale-url >`).
741
747
748
+ You can also use special attributes to configure them:
749
+
750
+ .. configuration-block ::
751
+
752
+ .. code-block :: php-annotations
753
+
754
+ // src/Controller/ArticleController.php
755
+
756
+ // ...
757
+ class ArticleController extends AbstractController
758
+ {
759
+ /**
760
+ * @Route(
761
+ * "/articles/{_locale}/search.{_format}",
762
+ * locale="en",
763
+ * format="html",
764
+ * fragment="first-result-id",
765
+ * requirements={
766
+ * "
8000
;_locale": "en|fr",
767
+ * "_format": "html|xml",
768
+ * }
769
+ * )
770
+ */
771
+ public function search()
772
+ {
773
+ }
774
+ }
775
+
776
+ .. code-block :: yaml
777
+
778
+ # config/routes.yaml
779
+ article_search :
780
+ path : /articles/{_locale}/search.{_format}
781
+ controller : App\Controller\ArticleController::search
782
+ locale : en
783
+ format : html
784
+ requirements :
785
+ _locale : en|fr
786
+ _format : html|xml
787
+
788
+ .. code-block :: xml
789
+
790
+ <!-- config/routes.xml -->
791
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
792
+ <routes xmlns =" http://symfony.com/schema/routing"
793
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
794
+ xsi : schemaLocation =" http://symfony.com/schema/routing
795
+ http://symfony.com/schema/routing/routing-1.0.xsd" >
796
+
797
+ <route id =" article_search"
798
+ path =" /articles/{_locale}/search.{_format}"
799
+ controller =" App\Controller\ArticleController::search"
800
+ locale =" en"
801
+ format =" html" >
802
+
803
+ <requirement key =" _locale" >en|fr</requirement >
804
+ <requirement key =" _format" >html|rss</requirement >
805
+ <requirement key =" year" >\d+</requirement >
806
+
807
+ </route >
808
+ </routes >
809
+
810
+ .. code-block :: php
811
+
812
+ // config/routes.php
813
+ namespace Symfony\Component\Routing\Loader\Configurator;
814
+
815
+ use App\Controller\ArticleController;
816
+
817
+ return function (RoutingConfigurator $routes) {
818
+ $routes->add('article_show', '/articles/{_locale}/search.{_format}')
819
+ ->controller([ArticleController::class, 'search'])
820
+ ->locale('en')
821
+ ->format('html)
822
+ ->requirements([
823
+ '_locale' => 'en|fr',
824
+ '_format' => 'html|rss',
825
+ 'year' => '\d+',
826
+ ])
827
+ ;
828
+ };
829
+
830
+ .. versionadded ::
831
+
832
+ The special attributes has been introduced in Symfony 4.3.
833
+
742
834
.. _routing-trailing-slash-redirection :
743
835
744
836
Redirecting URLs with Trailing Slashes
0 commit comments