Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf1eae44

Browse files
committed
added doc for defaults
1 parent06a71a9 commitf1eae44

File tree

1 file changed

+93
-1
lines changed

1 file changed

+93
-1
lines changed

‎routing.rst

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,10 @@ With all of this in mind, check out this advanced example:
623623
/**
624624
* @Route(
625625
* "/articles/{_locale}/{year}/{slug}.{_format}",
626-
* defaults={"_format": "html"},
626+
* defaults={
627+
* "_locale": "en",
628+
* "_format": "html"
629+
* },
627630
* requirements={
628631
* "_locale": "en|fr",
629632
* "_format": "html|rss",
@@ -643,6 +646,7 @@ With all of this in mind, check out this advanced example:
643646
path:/articles/{_locale}/{year}/{slug}.{_format}
644647
controller:App\Controller\ArticleController::show
645648
defaults:
649+
_locale:en
646650
_format:html
647651
requirements:
648652
_locale:en|fr
@@ -662,6 +666,7 @@ With all of this in mind, check out this advanced example:
662666
path="/articles/{_locale}/{year}/{slug}.{_format}"
663667
controller="App\Controller\ArticleController::show">
664668
669+
<defaultkey="_locale">en</default>
665670
<defaultkey="_format">html</default>
666671
<requirementkey="_locale">en|fr</requirement>
667672
<requirementkey="_format">html|rss</requirement>
@@ -681,6 +686,7 @@ With all of this in mind, check out this advanced example:
681686
$routes->add('article_show', '/articles/{_locale}/{year}/{slug}.{_format}')
682687
->controller([ArticleController::class, 'show'])
683688
->defaults([
689+
'_locale' => 'en',
684690
'_format' => 'html',
685691
])
686692
->requirements([
@@ -739,6 +745,92 @@ that are special: each adds a unique piece of functionality inside your applicat
739745
``_locale``
740746
Used to set the locale on the request (:ref:`read more<translation-locale-url>`).
741747

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+
* "_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+
<routesxmlns="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+
<routeid="article_search"
798+
path="/articles/{_locale}/search.{_format}"
799+
controller="App\Controller\ArticleController::search"
800+
locale="en"
801+
format="html">
802+
803+
<requirementkey="_locale">en|fr</requirement>
804+
<requirementkey="_format">html|rss</requirement>
805+
<requirementkey="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+
742834
.. _routing-trailing-slash-redirection:
743835

744836
Redirecting URLs with Trailing Slashes

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp