我希望通过可选_locale
为我的所有网址添加前缀。顺便说一句,我正在处理fr
和en
。
重点是,当_locale
给出时我会使用symfony Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest
,如果不是,我会使用保存在数据库中的语言环境谢谢到我自己的听众(优先级:15),它在前一个之后立即运行。
我到目前为止所尝试的内容如下:
应用程序/配置/ routing.yml中
entrypoint:
resource: 'locale_prefixed_routing.yml'
prefix: /{_locale}
locale_prefixed_routing.yml
导入捆绑路由。
/fr/url/example
有效但/url/example
没有,并返回404页。
如何告诉symfony使用url中的语言环境,如果不存在则使用数据库中的语言环境?
答案 0 :(得分:0)
I suppose your routes are defined like this in your locale_prefixed_routing.yml :
my_route:
path: /
defaults:
Actually routes like /url/example
without any locale are not defined in your routing because you ask to prefix all the routes loaded in locale_prefixed_routing.yml by the locale. So it is normal to get an error when requesting such a route without locale.
You can try to make another routing file named non_localized_routing.yml containing the same routes except that you will use specific defaults.
Example :
route_example:
path: /url/example
defaults:
_controller: FrameworkBundle:Redirect:urlRedirect
path: /{_locale}/url/example
permanent: true
Tell me if it permits you to get rid of your problem, I haven't test it yet.
答案 1 :(得分:-1)
对我来说,当用户没有提供语言环境时,将语言环境存储在数据库中听起来很奇怪。您只需添加默认值:
framework:
default_locale: en