Symfony3.3重定向到路由不采用定义的路径

时间:2017-07-02 14:28:58

标签: php symfony routing fosuserbundle symfony-3.3

我正在使用FOSUserBundle,我只想在其所有路线前加上' / account'。 我有一个继承自FOSUserBundle的UserBundle:

<?php
//UserBundle.php

namespace UserBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class UserBundle extends Bundle
{
    public function getParent()
    {
        return 'FOSUserBundle';
    }
}

在我的路线中使用前缀定义此控制器:

# app/config/routing.yml
user:
    resource: "@UserBundle/Resources/config/routing.yml"
    prefix:   /account

并在我的包中导入FOS路线:

#UserBundle/config/routing.yml
fos_user:
    resource: "@FOSUserBundle/Resources/config/routing/all.xml"
    prefix:   /user

我尝试逐个文件覆盖文件,并逐个覆盖所有路由,通过XML(覆盖父文件)或通过yml。我总是很好地定义了路由(bin / console debug的输出:route --show-controllers):

  fos_user_security_login             GET|POST   ANY      ANY    /account/login                             FOSUserBundle:Security:login                          
  fos_user_security_check             POST       ANY      ANY    /account/login_check                       FOSUserBundle:Security:check                          
  fos_user_security_logout            GET|POST   ANY      ANY    /account/logout                            FOSUserBundle:Security:logout                         
  fos_user_profile_show               GET        ANY      ANY    /account/profile/                          FOSUserBundle:Profile:show                            
  fos_user_profile_edit               GET|POST   ANY      ANY    /account/profile/edit                      FOSUserBundle:Profile:edit                            
  fos_user_registration_register      GET|POST   ANY      ANY    /account/register/                         FOSUserBundle:Registration:register                   
  fos_user_registration_check_email   GET        ANY      ANY    /account/register/check-email              FOSUserBundle:Registration:checkEmail                 
  fos_user_registration_confirm       GET        ANY      ANY    /account/register/confirm/{token}          FOSUserBundle:Registration:confirm                    
  fos_user_registration_confirmed     GET        ANY      ANY    /account/register/confirmed                FOSUserBundle:Registration:confirmed                  
  fos_user_resetting_request          GET        ANY      ANY    /account/resetting/request                 FOSUserBundle:Resetting:request                       
  fos_user_resetting_send_email       POST       ANY      ANY    /account/resetting/send-email              FOSUserBundle:Resetting:sendEmail                     
  fos_user_resetting_check_email      GET        ANY      ANY    /account/resetting/check-email             FOSUserBundle:Resetting:checkEmail                    
  fos_user_resetting_reset            GET|POST   ANY      ANY    /account/resetting/reset/{token}           FOSUserBundle:Resetting:reset                         
  fos_user_change_password            GET|POST   ANY      ANY    /account/profile/change-password           FOSUserBundle:ChangePassword:changePassword

但是当我重定向到路线时,fos_user_security_login&#39;我被重定向到&#39; / login&#39;而不是&#39; / account / login&#39;

如果你知道为什么,我会很高兴知道:p 编辑:这是security.yml中的防火墙和acls

firewalls:
    # disables authentication for assets and the profiler, adapt it according to your needs
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false
    main:
        pattern: ^/
        form_login:
            check_path: /account/login_check
            provider: fos_userbundle
            csrf_token_generator: security.csrf.token_manager
            # if you are using Symfony < 2.8, use the following config instead:
            # csrf_provider: form.csrf_provider
        logout:
            path:   /account/logout
            target: /account/login
        anonymous:    true

access_control:
    # authorise to login and reset password for everyone
    - { path: ^/account, role: IS_AUTHENTICATED_ANONYMOUSLY }

2 个答案:

答案 0 :(得分:1)

我发现了问题,我是因为权利管理不善。 我尝试访问/并将其重定向到/account/login,但我没有为/路径设置ACL,这意味着它会自动将我重定向到/login,告诉我问题在于我的重定向。

答案 1 :(得分:0)

可能问题在于

#UserBundle/config/routing.yml
fos_user:
    resource: "@FOSUserBundle/Resources/config/routing/all.xml"
    prefix:   /user

它可能会覆盖您默认使用该路由定义的登录路由。尝试在路由文件中添加:

login:
    path: /account/login
    defaults: { _controller: FOSUserBundle:Security:login }

login_check:
    path:  /account/login_check
    defaults: { _controller: FOSUserBundle:Security:check }

导入所有路由后,或仅从FOSUserBundle添加所需的路由。您可以从供应商目录下的FOSUserBundle / Resources / config / routing / all.xml获取它们。