首页 文章

symfony2.0.0在security.yml中获取语言环境

提问于
浏览
3

我正在使用symfony2.0.0 . 我没有机会更新symfony . 这是我的security.yml

firewalls:
        main:
          logout: true
          pattern: .*
          http_basic: true
          anonymous: true
          form_login:
            provider: fos_userbundle
            login_path: /login
            use_forward:  true
            check_path: /login_check
            failure_path: /login_fail
          remember_me:
            key:      "lkjxd%34(lksdf;SDfsf"
            lifetime: 31536000
            path:     /
            domain:   ~

如何为我的 failure_path 使用区域设置 . 试图使用/%locaale%/ failure_path,但它总是返回en(我的默认语言环境) . 它不了解路由名称 . 如果我使用路线的名称,例如 login_fail 它不起作用(重定向像相对路径) .

这是我的路线 .

login_fail:
  pattern: /{_locale}/login_fail
  defaults: { _controller: ContactbeeProfileBundle:Profile:dashboard, _locale: en }

有什么想法来解决它吗?

1 回答

  • 1

    在实际处理请求之前编译DIC之前加载配置,因为它可以从 Requestyou can't access locale in configuration 访问 .

    如果您不介意一个额外的重定向,您可以执行一个操作,在验证失败后将用户重定向到正确的路由:

    namespace Acme\DemoBundle\Controller;
    
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    
    class SecurityController extends Controller {
        public function failedAction() {
            return $this->redirect($this->generateUrl('login_fail_localized'), [
                'locale' => $this->getRequest()->getLocale()
            ]);
        }
    }
    

    设置故障路径以匹配其路由:

    # routing.yml
    login_fail:
        pattern: /login_fail
            defaults: { _controller: AcmeDemoBundle:Security:failed }
    
    login_fail_localized:
        pattern: /{locale}/login_fail
            defaults: { _controller: ContactbeeProfileBundle:Profile:dashboard }
    

相关问题