10

我正在尝试让语言环境切换在我的应用程序的登录屏幕中工作。为此,我在登录页面上有指向 site.com/(默认语言环境)和 site.com/en(我支持的第二个语言环境)的链接。一旦我登录,切换就像一个魅力。但是,如果我尚未通过身份验证,登录总是会返回到默认语言环境。我的理解是,如果我使用 FOSUserBundle 中的命名路由,那么它应该能够自动处理语言环境,但我无法让它工作。

我的 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

# 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

任何指针都非常感谢,因为我已经坚持了几天了

4

4 回答 4

12

解决方案最终是将语言环境添加到 FOSUserBundle 的路由导入前:

# FOS User bundle
fos_user_security:
    resource: "@FOSUserBundle/Resources/config/routing/security.xml"
    prefix: /{_locale}

fos_user_profile:
    resource: "@FOSUserBundle/Resources/config/routing/profile.xml"
    prefix: /{_locale}/profile

fos_user_register:
    resource: "@FOSUserBundle/Resources/config/routing/registration.xml"
    prefix: /{_locale}/register

fos_user_resetting:
    resource: "@FOSUserBundle/Resources/config/routing/resetting.xml"
    prefix: /{_locale}/resetting

fos_user_change_password:
    resource: "@FOSUserBundle/Resources/config/routing/change_password.xml"
    prefix: /{_locale}/profile

并更改防火墙以允许匿名路由中的语言环境并配置 logout_path:

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:
                path:   fos_user_security_logout
            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: ^/[a-z]+/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/[a-z]+/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/[a-z]+/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin, role: ROLE_ADMIN }
        - { path: ^/, role: ROLE_USER } # this must be the last match, as url patterns are checked in the order they appear

从长远来看,使用 JMSI18nRoutingBundle 可能会更好,但是当我尝试它时,它并没有作为解决方案的一部分起作用,而且这个项目的预算不允许我开始弄清楚为什么不这样做,所以这将留给未来的更新。

于 2013-08-05T13:18:15.387 回答
7

我不知道您如何处理语言环境检测/切换,但使用JMSI18nRoutingBundle您可以执行以下操作。

将所需的捆绑包添加到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(),
);

修改现有路由以在它们前面加上所需的语言环境:

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 }

现在它应该工作了!

于 2013-06-20T08:05:10.627 回答
2

我的解决方案:您必须为 be_simple_i18n 类型格式重新定义 fos_user 路由。(我选择了yaml版本):

在 app/config/routing.yml

[...]
#register the path to the file with the be_simple_i18n type fos_user routes
fos_user:       
    resource: "config/routing/fos_user_i18n.yml"
    type: be_simple_i18n

my_yaml_i18n_routes:
    resource: "config/routing/i18n.yml"
    type: be_simple_i18n

在 app/Resources/config/routing/fos_user_i18n.yml 中:

#you have to make entries for all the fos_user routes here!
#you can find them all in vendor/friendsofsymfony/user-bundle/Resources/routing
fos_user_security_login:
    locales:  { en: "/login", de: "/anmelden" }
    defaults: { _controller: FOSUserBundle:Security:login }
fos_user_security_check:
    locales:  { en: "/login_check", de: "/login_pruefung" }
    defaults: { _controller: FOSUserBundle:Security:check }
#...

在 app/config/security.yml

firewalls:       
    [...]

    main:
        pattern: ^/
        form_login:
            provider: fos_userbundle
            login_path: fos_user_security_login
            check_path: fos_user_security_check
            csrf_provider: security.csrf.token_manager # Use form.csrf_provider instead for Symfony <2.4

        logout:
            path: fos_user_security_logout
            target: #where_no_one_has_gone_before
        [...]

access_control:
    - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/anmelden$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/registrieren, role: IS_AUTHENTICATED_ANONYMOUSLY }
    [...]
于 2015-08-07T09:57:49.530 回答
0

我一直在努力解决这个问题。我希望将纯登录(会话中没有 URL)重定向到正确的本地化页面。我可以在 Symfony 文档中找到答案:

# app/config/security.yml
security:
# ...
 firewalls:
    main:
        form_login:
            # ...
            default_target_path: index [ or your named route ]
于 2016-01-28T01:38:23.687 回答