如何自定义 FOS UserBundle URL

Posted

技术标签:

【中文标题】如何自定义 FOS UserBundle URL【英文标题】:How to customize FOS UserBundle URLs 【发布时间】:2013-11-05 16:05:44 【问题描述】:

我已经安装了FOSUserBundle,我想将网址自定义为/account/login/account/register/account/logout,而不是/login/register/logout

我知道我可以修改捆绑包的路由配置,但这似乎不是正确的方法。

【问题讨论】:

或将整个路由xml从fosUserBundle复制到您的应用程序文件夹并在那里自定义它们或更改他们在install docs的步骤6中建议的前缀 .htaccess 没有工作 - 完全错误的方向!!您可以复制路线并仅包含已更改的路线,也可以按照我的回答中所述仅覆盖单个路线。 【参考方案1】:

如何覆盖/更改 FOSUserBundle 的路由

您可以在将 FOSUserBundle 的 XML 路由作为资源导入之后重新声明 来覆盖 app/config/routing.yml 中的 /register 路由。

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

# ...

fos_user_registration_register:
    path:      /account/register
    defaults:   _controller: FOSUserBundle:Registration:register 

...或者只是在导入时更改前缀:

fos_user_register:
    resource: "@FOSUserBundle/Resources/config/routing/registration.xml"
    prefix: /account/register

# no need to override the route

/login/logout 也是如此:

fos_user_security:
    resource: "@FOSUserBundle/Resources/config/routing/security.xml"

# ...

fos_user_security_login:
    path:      /account/login
    defaults:   _controller: FOSUserBundle:Security:login, _method: POST 

fos_user_security_logout:
    path:      /account/login
    defaults:   _controller: FOSUserBundle:Security:logout, _method: POST 

另一种覆盖登录和注销 url 的方法

登录和注销路径也可以直接在您的app/config/security.yml 中配置:

security:
    firewalls: 
        your_firewall:
            # ...
            form_login:
                login_path: /account/login  # instead of fos_user_security_login
                # ...
            logout:
                path: /account/logout       # instead of fos_user_security_logout
                # ...

YAML 格式的 FOSUserBundle 的所有路由列表

您可以直接更改然后将它们包含在您的app/config/routing.yml 中(无需导入捆绑包提供的资源作为资源)......或者将它们全部放入一个文件并将其作为资源包含...... .

# -> from @FOSUserBundle/Resources/routing/change_password.xml

fos_user_change_password:
    pattern: /profile/password/change
    defaults:  _controller: FOSUserBundle:ChangePassword:changePassword 
    requirements:
        _method: GET|POST

# -> from @FOSUserBundle/Resources/routing/group.xml

fos_user_group_list:
    pattern: /groups/list
    defaults:  _controller: FOSUserBundle:Group:list 
    requirements:
        _method: GET

fos_user_group_new:
    pattern: /groups/new
    pattern:
    defaults:  _controller: FOSUserBundle:Group:new 
    requirements:
        _method: GET

fos_user_group_show:
    pattern: /groups/groupname
    defaults:  _controller: FOSUserBundle:Group:show 
    requirements:
        _method: GET

fos_user_group_edit:
    pattern: /groups/groupname/edit
    defaults:  _controller: FOSUserBundle:Group:edit 
    requirements:
        _method: GET|POST

fos_user_group_delete:
    pattern: /groups/groupname/delete
    defaults:  _controller: FOSUserBundle:Group:delete 
    requirements:
        _method: GET

# -> from @FOSUserBundle/Resources/routing/profile.xml

fos_user_profile_show:
    pattern: /profile/show
    defaults:  _controller: FOSUserBundle:Profile:show 
    requirements:
        _method: GET

fos_user_profile_edit:
    pattern: /profile/edit
    defaults:  _controller: FOSUserBundle:Profile:edit 
    requirements:
        _method: GET|POST

# -> from @FOSUserBundle/Resources/routing/registration.xml

fos_user_registration_register:
    pattern: /registration
    defaults:  _controller: FOSUserBundle:Registration:register 
    requirements:
        _method: GET|POST

fos_user_registration_check_email:
    pattern: /registration/check-email
    defaults:  _controller: FOSUserBundle:Registration:checkEmail 
    requirements:
        _method: GET

fos_user_registration_confirm:
    pattern: /registration/confirm/token
    defaults:  _controller: FOSUserBundle:Registration:confirm 
    requirements:
        _method: GET

fos_user_registration_confirmed:
    pattern: /registration/confirmed
    defaults:  _controller: FOSUserBundle:Registration:confirmed 
    requirements:
        _method: GET

# -> from @FOSUserBundle/Resources/routing/resetting.xml

fos_user_resetting_request:
    pattern: /profile/password/reset
    defaults:  _controller: FOSUserBundle:Resetting:request 
    requirements:
        _method: GET

fos_user_resetting_send_email:
    pattern: /profile/password/reset
    defaults:  _controller: FOSUserBundle:Resetting:sendEmail 
    requirements:
        _method: POST

fos_user_resetting_check_email:
    pattern: /profile/password/reset/check-email
    defaults:  _controller: FOSUserBundle:Registration:checkEmail 
    requirements:
        _method: GET

fos_user_resetting_reset:
    pattern: /profile/password/reset/token
    defaults:  _controller: FOSUserBundle:Registration:reset 
    requirements:
        _method: GET|POST

# -> from @FOSUserBundle/Resources/routing/security.xml

fos_user_security_login:
    pattern: /login
    defaults:  _controller: FOSUserBundle:Security:login 
    requirements:
        _method: GET|POST

fos_user_security_check:
    pattern: /login_check
    defaults:  _controller: FOSUserBundle:Security:check 

fos_user_security_logout:
    pattern: /logout
    defaults:  _controller: FOSUserBundle:Security:logout 
    requirements:
        _method: GET|POST

【讨论】:

您需要设置登录路径和注销路径 app/config/security.yml 才能正确重定向。我认为这一步是强制性的 pattern_method 自 2.2 版起已弃用。 nifir,您的路径中有错误。在“重置”组中,您使用FOSUserBundle:Registration,这将在所有路径上报错。 感谢您的精彩回答。我更正了几个小错误并添加了 Symfony 3 版本(将 Symfony 2 版本保留在那里)。它只是在等待同行评审。

以上是关于如何自定义 FOS UserBundle URL的主要内容,如果未能解决你的问题,请参考以下文章

显示配置文件 FOS UserBundle + Sonata UserBundle

FOS UserBundle 无法登录

Symfony 4/JMS/FOSUser:无法从 FOS\UserBundle 序列化数据

FOS UserBundle 用户管理器 - updateUser 后登录

服务“security.authentication.manager”依赖于不存在的服务“security.user.provider.concrete.fos_userbundle”

Sf2 : FOS UserBundle : 注册 AJAX