FOSUserBundle登录时的区域设置切换 [英] Locale switch in login of FOSUserBundle
问题描述
我正在尝试在应用程序的登录屏幕中进行语言环境切换.为此,我在登录页面上具有指向site.com/(默认语言环境)和site.com/en(我支持的第二个语言环境)的链接.登录后,切换就像超级按钮一样.但是,如果我尚未通过身份验证,则登录名始终会返回到默认语言环境.我的理解是,如果我使用FOSUserBundle中的命名路由,那么它应该能够自动处理语言环境,但是我无法使其正常工作.
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.
我的app/config/security.yml
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 }
我的app/config/routing.yml
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
推荐答案
I don't know how you're handling the locale detection/switch but with JMSI18nRoutingBundle you can do as below.
将所需的捆绑包添加到composer.json
:
Add the required bundles to composer.json
:
"require": {
...
"jms/i18n-routing-bundle": "1.1.*",
"jms/translation-bundle": "1.1.*",
"friendsofsymfony/user-bundle": "1.3.*"
},
配置捆绑包:
jms_i18n_routing:
default_locale: en
locales: [en, fr, it, sp]
strategy: prefix
引导捆绑包:
$bundles = array(
...
new JMS\I18nRoutingBundle\JMSI18nRoutingBundle(),
new FOS\UserBundle\FOSUserBundle(),
);
修改现有路由,以所需的语言环境作为前缀:
Modify existing routes to prefix them with the desired locale:
access_control:
# Routes are prefixed by ther user locale.
- { 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 }
现在应该可以了!
这篇关于FOSUserBundle登录时的区域设置切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!