可以在routes.yaml中全局定义defaults/requirements吗?

huangapple go评论58阅读模式
英文:

Is it possible to define routes.yaml defaults/requirements globally?

问题

在Symfony 6的routes.yaml中,我有以下配置:

somepage1:
  path: /{_locale}/somepage1
  controller: App\Controller\PageController::somepage1
  defaults:
    _locale: en
  requirements:
    _locale: en|fr|de

somepage2:
  path: /{_locale}/somepage2
  controller: App\Controller\PageController::somepage2
  defaults:
    _locale: en
  requirements:
    _locale: en|fr|de

由于会有很多路由,是否可能更全局地定义defaults和requirements,以便它们适用于使用_locale变量的所有URL?这样可以避免在添加新语言时为所有路由输入这些内容。

英文:

In Symfony 6 routes.yaml I have:

somepage1:
  path: /{_locale}/somepage1
  controller: App\Controller\PageController::somepage1
  defaults:
    _locale: en
  requirements:
    _locale: en|fr|de

somepage2:
  path: /{_locale}/somepage2
  controller: App\Controller\PageController::somepage2
  defaults:
    _locale: en
  requirements:
    _locale: en|fr|de

As there will be many routes, is it possible to define the defaults and requirements more globally, so that they get applied to all URLs that use the _locale variable? This is to avoid entering this for all the routes when new languages are added.

答案1

得分: 1

是的,所有内容都在这里写好了。
https://symfony.com/doc/6.2/the-fast-track/en/28-intl.html

您需要创建一个新的参数:
app.supported_locales: 'en|fr|de'

如果上述解决方案对您不起作用,那么您需要使用 RequestListener 来检查支持的区域设置。

英文:

Yes, everything is written here.
https://symfony.com/doc/6.2/the-fast-track/en/28-intl.html

You need to create a new parameter:
app.supported_locales: 'en|fr|de'


If above solution doesn't work for you, then you need to use RequestListener to check for supported locales

huangapple
  • 本文由 发表于 2023年7月3日 19:03:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76604137.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定