schmittjoh/JMSI18nRoutingBundle — Issue #15: Locale not being prefixed when redirecting to /login*
Author: NeuralClone
Created: 2011-12-20 20:39:04+00:00
State: open

Hi,

I seem to be having an issue using the prefix strategy when using this bundle with Symfony 2.0.7 (standard edition). I'm not sure if this is an issue with the bundle, a change in FOSUserBundle (I use that as my user provider), or a change in Symfony itself.

The locale is correctly prefixed in all of my routes *except* when trying to directly access the secured section of my site when not logged in. Instead of redirecting to `/{_locale}/login`, I keep getting redirected to `/login`, which causes Symfony to throw a `NotFoundHttpException` due to the route not existing.

For example, if a user tries to access `/en/members` before they've logged in, it redirects them to `/login` instead of `/en/login`. The same thing happens when trying to access `/fr/members/`. As far as I can tell, this is a fairly new problem that just started in Symfony 2.0.7. It may also impact 2.0.6 but I haven't tested it on that version. I'm pretty confident that it was working in 2.0.6.

Presumably there's an issue with the way I have my firewall configured or there's an issue with another part of my configuration. This does seem a bit odd though since the locale is correctly prefixed in every other situation (as far as I can tell).

In config.yml:

jms_i18n_routing:
default_locale: %locale%
locales: [en, fr]
strategy: prefix

In my security.yml:

firewalls:
frontend:
pattern: ^/.*
provider: fos_userbundle
form_login:
check_path: /login_check
login_path: /login
logout:
path: /logout
target: /
anonymous: true

access_control:
- { path: ^/[a-z][a-z]/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/[a-z][a-z]/login_check$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/[a-z][a-z]/members, role: ROLE_MEMBER }
- { path: ^/.*, role: IS_AUTHENTICATED_ANONYMOUSLY}

After running `php app/console router:debug` I get the following security routes:

en_fos_user_security_login ANY /en/login
fr_fos_user_security_login ANY /fr/login
en_fos_user_security_check ANY /en/login_check
fr_fos_user_security_check ANY /fr/login_check
en_fos_user_security_logout ANY /en/logout
fr_fos_user_security_logout ANY /fr/logout

I've currently implemented a simple workaround that allows users to login without an exception being thrown by explicitly setting the default locale in my firewall:

firewalls:
frontend:
pattern: ^/.*
provider: fos_userbundle
form_login:
check_path: /%locale%/login_check
login_path: /%locale%/login
logout:
path: /%locale%/logout
target: /
anonymous: true

But as you can imagine, this is less than ideal since it *always* uses the default locale when prompting users to login. Thanks for your help and/or input.

Keep up the great work. This bundle has saved me tons of development time. :)