简体   繁体   English

Symfony2将简单路由重定向到默认语言

[英]Symfony2 redirect simple routes to default language

How are you doing? 你好吗? I'm developing a multilingual platform with Symfony2. 我正在使用Symfony2开发多语言平台。 If someone types in www.url.com/project I want to have it redirected to www.url.com/en/project. 如果有人输入www.url.com/project,我希望将其重定向到www.url.com/en/project。 Whats the easiest way to do so without repeating a method and all the routes a couple of times? 没有几次重复所有方法和路线的最简单方法是什么?

For the main url www.url.com to www.url.com/en/ I use the following controller 对于主网址www.url.com到www.url.com/en/,我使用以下控制器

    public function rootAction(Request $request) {
    $locale = $request -> getLocale();
    return $this -> redirect($this -> generateUrl('dbe_underConstructionLang', array('_locale' => $locale)));
    }

And here the routes: The default: 这是路由:默认值:

dbe_underConstruction:
    path:     /
    defaults: { _controller: DbeBundle:UnderConstruction:root }

This is a default page 这是默认页面

dbe_aboutus:
    path:     /{_locale}/aboutus
    defaults: { _controller: DbeBundleBundle:Aboutus:index }
    requirements:
        _locale: en|fr|de

Furthermore I was not able to make the login route multilingual because in the security.yml I was not able to set the required _locale parameter: 此外,由于在security.yml中,我无法设置所需的_locale参数,因此我无法使登录路由成为多语言的:

dbe_login:
path:     /login
defaults: { _controller: DbeBundle:Login:login}

 firewalls:
    secured_area:
        pattern:    ^/
        form_login:
            check_path: /login_check
            login_path: /login
        logout:
            path:   /logout
            target: /

Thanks already! 已经谢谢你了!

For me, best solution is to use JMSI18RoutingBundle . 对我来说,最好的解决方案是使用JMSI18RoutingBundle There are some configurations in config.yml and nothing else - no changes in routing.yml. config.yml中有一些配置,仅此而已-routing.yml中没有任何更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM