将可选_locale添加到路由

时间:2017-05-16 08:46:44

标签: symfony

我希望通过可选_locale为我的所有网址添加前缀。顺便说一句,我正在处理fren

重点是,当_locale给出时我会使用 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中的语言环境,如果不存在则使用数据库中的语言环境?

2 个答案:

答案 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