Anton G
Anton G

Reputation: 129

Symfony 2 get locale in security.yml

I'm using Symfony 2. I don't have a chance to update symfony. This is my 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
            <b>failure_path: /login_fail</b>
          remember_me:
            key:      "lkjxd%34(lksdf;SDfsf"
            lifetime: 31536000
            path:     /
            domain:   ~

How can I use locale for my failure_path. Tried to use /%locaale%/failure_path but it always return en(my default locale). It does not understand route names. If I use the name of the route e.g. login_fail it does not work(redirecting like a relative path).

This is my route.

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

Any ideas to fix it?

Upvotes: 3

Views: 1320

Answers (1)

tomas.pecserke
tomas.pecserke

Reputation: 3260

Configuration is loaded before DIC is compiled before the request is actually processed, as it's accessible from Request, you can't access locale in configuration.

If you don't mind one extra redirect, you can have an action that will redirect user after failed authentication to correct route:

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()
        ]);
    }
}

Set failure path to match it's route:

# routing.yml
login_fail:
    pattern: /login_fail
        defaults: { _controller: AcmeDemoBundle:Security:failed }

login_fail_localized:
    pattern: /{locale}/login_fail
        defaults: { _controller: ContactbeeProfileBundle:Profile:dashboard }

Upvotes: 1

Related Questions