I'm trying to get locale switching to work in the login screen of my application. In order to do that I have links on my login page that point to site.com/ (the default locale) and site.com/en (the second locale I support). As soon as I've logged in, the switching works like a charm. However if I'm not yet authenticated the login always goes back to the default locale. My understanding was that if I use the named routes from FOSUserBundle then it's should be able to handle the locales automatically, but I can't get it to work.
My app/config/security.yml
security:
encoders:
FOS\UserBundle\Model\UserInterface: sha512
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: ROLE_ADMIN
providers:
fos_userbundle:
id: fos_user.user_provider.username
firewalls:
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_provider: form.csrf_provider
login_path: fos_user_security_login
check_path: fos_user_security_check
logout: true
anonymous: true
access_control:
- { path: ^/_wdt, roles: IS_AUTHENTICATED_ANONYMOUSLY } # allow wdt for debugging
- { path: ^/_profiler/, role: IS_AUTHENTICATED_ANONYMOUSLY } # allow profiler for debugging
- { path: ^/bundles/, role: IS_AUTHENTICATED_ANONYMOUSLY } # allow assets to be loaded anonymously
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin, role: ROLE_ADMIN }
- { path: ^/, role: ROLE_USER }
My app/config/routing.yml
# FOS User bundle
fos_user_security:
resource: "@FOSUserBundle/Resources/config/routing/security.xml"
fos_user_profile:
resource: "@FOSUserBundle/Resources/config/routing/profile.xml"
prefix: /profile
#fos_user_register:
# resource: "@FOSUserBundle/Resources/config/routing/registration.xml"
# prefix: /register
fos_user_resetting:
resource: "@FOSUserBundle/Resources/config/routing/resetting.xml"
prefix: /resetting
fos_user_change_password:
resource: "@FOSUserBundle/Resources/config/routing/change_password.xml"
prefix: /profile
Any pointers much appreciated as I've been stuck with this for a couple of days now
My solution: You have to redefine the fos_user routes for the be_simple_i18n type format. (I chose the yaml version):
In app/config/routing.yml
In app/Resources/config/routing/fos_user_i18n.yml:
In app/config/security.yml
I have been struggling a bit with this issue. I wanted to have the pure login (no URL in the session) to be redirected to the proper localized page. I could find the answer in the Symfony documentation:
The solution ended up being to prefix the locale to the route imports of FOSUserBundle:
And also change the firewall to allow locales in the anonymous routes and configure the logout_path:
Using the JMSI18nRoutingBundle would probably be better in the long term, but it did not work as a drop in solution when I tried it and the budget for this project did not allow me to start figuring out why not, so that will be left for a future update.
I don't know how you're handling the locale detection/switch but with JMSI18nRoutingBundle you can do as below.
Add the required bundles to
composer.json
:Configure the bundles:
Bootstrap the bundles:
Modify existing routes to prefix them with the desired locale:
Now it should work!