如何调试(和修复)Symfony2|3 路由?
How to debug (and fix) Symfony2|3 routes?
我在 Symfony 2.8 应用程序中的 app/config/routing.yml
定义了这条路线:
platform_chat:
resource: "@PlatformChatBundle/Controller/"
type: annotation
prefix: /chat
platform_admin:
resource: "@PlatformAdminBundle/Controller/"
type: annotation
prefix: /admin
#----> this is part of routing.yml but I forgot to add it
easy_admin_bundle:
resource: "@PlatformAdminBundle/Controller/AdminController.php"
type: annotation
prefix: /admin
#FOSUser
fos_user:
resource: "@FOSUserBundle/Resources/config/routing/all.xml"
您可能已经注意到 PlatformAdminBundle
是后端,PlatformChatBundle
是前端。考虑到这一点,我尝试设置并为两者使用一个防火墙,然后在 security.interactive_login
事件上重定向到正确的路由|路径。这是防火墙的样子:
security:
...
role_hierarchy:
ROLE_CHATTER: ROLE_USER
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: ROLE_ADMIN
...
firewalls:
...
ignored:
pattern: ^/(login(_check)?|logout|resetting)$
security: false
global:
pattern: ^/admin/(.*)|^/chat/(.*)
provider: fos_userbundle
form_login:
csrf_provider: security.csrf.token_manager
login_path: fos_user_security_login
check_path: fos_user_security_check
# if true, forward the user to the login form instead of redirecting
use_forward: true
# login success redirecting options (read further below)
always_use_default_target_path: true
default_target_path: /admin
target_path_parameter: _target_path
use_referer: true
remember_me: true
logout: ~
remember_me:
secret: '%secret%'
lifetime: 604800 # 1 week in seconds
path: /
access_control:
- { path: ^/chat/, role: ROLE_CHATTER }
- { path: ^/admin/, role: ROLE_ADMIN }
但它不起作用,因为当我尝试以任何一个用户身份登录时,我以这个错误结束:
You must configure the check path to be handled by the firewall using form_login in your security firewall configuration.
这让我觉得路由或防火墙配置不正确。我已经检查了调试工具栏下的路由和 none 匹配,所以它们是错误的。我已阅读文档 here but it's not helpful at all and I am not getting the fix for the problem. You can take this post as a second part of ,但我不想更改旧文档的主题和内容,因为我认为这对其他人的未来会有帮助。那么,有什么建议吗?你会调试这种与路由相关的问题吗?对我的特定问题有什么解决办法吗?我真的卡在这里了!
更新
我已经按照@xabbuh 的建议进行了更改,所以现在 app/config/routing.yml
看起来像:
platform_chat:
resource: "@PlatformChatBundle/Controller/"
type: annotation
prefix: /chat
options:
expose: true
platform_admin:
resource: "@PlatformAdminBundle/Controller/"
type: annotation
prefix: /admin
options:
expose: true
#EasyAdminBundle
easy_admin_bundle:
resource: "@PlatformAdminBundle/Controller/AdminController.php"
type: annotation
prefix: /admin
options:
expose: true
#FOSUser
fos_user:
resource: "@FOSUserBundle/Resources/config/routing/all.xml"
#FOSUser Groups
fos_user_group:
resource: "@FOSUserBundle/Resources/config/routing/group.xml"
prefix: /group
#FOSJsRouting
fos_js_routing:
resource: "@FOSJsRoutingBundle/Resources/config/routing/routing.xml"
和``看起来像:
security:
...
firewalls:
...
global:
pattern: /
anonymous: true
provider: fos_userbundle
form_login:
csrf_provider: security.csrf.token_manager
login_path: fos_user_security_login
check_path: fos_user_security_check
use_forward: true # if true, forward the user to the login form instead of redirecting
always_use_default_target_path: true # login success redirecting options (read further below)
default_target_path: /admin
target_path_parameter: _target_path
use_referer: true
remember_me: true
logout: ~
remember_me:
secret: '%secret%'
lifetime: 604800 # 1 week in seconds
path: /
access_control:
- { path: ^/chat/, role: ROLE_CHATTER }
- { path: ^/admin/, role: ROLE_ADMIN }
清除缓存后,这是我的尝试和结果:
- 以
ROL_CHATTER
身份登录:我将按预期 http://domain.tld/app_dev.php/chat/
获取登录表单并使用有效凭据 我收到以下消息:拒绝访问。你是 CHATTER。这是正确的,因为我在 security.interactive_login
上有一个监听器,这就是当用户使用这些凭据登录时我正在做的事情。
- 以
ROL_ADMIN
身份登录:我将按预期 http://domain.tld/app_dev.php/admin/
获取登录表单并使用有效凭据 我收到以下消息:凭据错误.这是错误的,因为凭据是有效的,至少我应该收到另一条消息(拒绝访问。你是管理员),因为 security.interactive_login
上的侦听器但正如我所说,这不是什么正在发生。
与监听器相关的信息在 上。怎么了?
您的问题是用于匹配 global
防火墙请求的正则表达式是 /admin/(.*)|^/chat/(.*)
,但您的检查路径是 /login_check
。如您所见,该路径不会与您的防火墙匹配,从而导致您发布错误消息。
如果我是你,我会简单地删除登录相关内容的防火墙,并将 global
防火墙的正则表达式更改为 /
。然后您只需添加 anonymous: true
以便未登录的用户能够访问登录表单。您的访问控制部分仍会拒绝访问您的保护区。
我在 Symfony 2.8 应用程序中的 app/config/routing.yml
定义了这条路线:
platform_chat:
resource: "@PlatformChatBundle/Controller/"
type: annotation
prefix: /chat
platform_admin:
resource: "@PlatformAdminBundle/Controller/"
type: annotation
prefix: /admin
#----> this is part of routing.yml but I forgot to add it
easy_admin_bundle:
resource: "@PlatformAdminBundle/Controller/AdminController.php"
type: annotation
prefix: /admin
#FOSUser
fos_user:
resource: "@FOSUserBundle/Resources/config/routing/all.xml"
您可能已经注意到 PlatformAdminBundle
是后端,PlatformChatBundle
是前端。考虑到这一点,我尝试设置并为两者使用一个防火墙,然后在 security.interactive_login
事件上重定向到正确的路由|路径。这是防火墙的样子:
security:
...
role_hierarchy:
ROLE_CHATTER: ROLE_USER
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: ROLE_ADMIN
...
firewalls:
...
ignored:
pattern: ^/(login(_check)?|logout|resetting)$
security: false
global:
pattern: ^/admin/(.*)|^/chat/(.*)
provider: fos_userbundle
form_login:
csrf_provider: security.csrf.token_manager
login_path: fos_user_security_login
check_path: fos_user_security_check
# if true, forward the user to the login form instead of redirecting
use_forward: true
# login success redirecting options (read further below)
always_use_default_target_path: true
default_target_path: /admin
target_path_parameter: _target_path
use_referer: true
remember_me: true
logout: ~
remember_me:
secret: '%secret%'
lifetime: 604800 # 1 week in seconds
path: /
access_control:
- { path: ^/chat/, role: ROLE_CHATTER }
- { path: ^/admin/, role: ROLE_ADMIN }
但它不起作用,因为当我尝试以任何一个用户身份登录时,我以这个错误结束:
You must configure the check path to be handled by the firewall using form_login in your security firewall configuration.
这让我觉得路由或防火墙配置不正确。我已经检查了调试工具栏下的路由和 none 匹配,所以它们是错误的。我已阅读文档 here but it's not helpful at all and I am not getting the fix for the problem. You can take this post as a second part of
更新
我已经按照@xabbuh 的建议进行了更改,所以现在 app/config/routing.yml
看起来像:
platform_chat:
resource: "@PlatformChatBundle/Controller/"
type: annotation
prefix: /chat
options:
expose: true
platform_admin:
resource: "@PlatformAdminBundle/Controller/"
type: annotation
prefix: /admin
options:
expose: true
#EasyAdminBundle
easy_admin_bundle:
resource: "@PlatformAdminBundle/Controller/AdminController.php"
type: annotation
prefix: /admin
options:
expose: true
#FOSUser
fos_user:
resource: "@FOSUserBundle/Resources/config/routing/all.xml"
#FOSUser Groups
fos_user_group:
resource: "@FOSUserBundle/Resources/config/routing/group.xml"
prefix: /group
#FOSJsRouting
fos_js_routing:
resource: "@FOSJsRoutingBundle/Resources/config/routing/routing.xml"
和``看起来像:
security:
...
firewalls:
...
global:
pattern: /
anonymous: true
provider: fos_userbundle
form_login:
csrf_provider: security.csrf.token_manager
login_path: fos_user_security_login
check_path: fos_user_security_check
use_forward: true # if true, forward the user to the login form instead of redirecting
always_use_default_target_path: true # login success redirecting options (read further below)
default_target_path: /admin
target_path_parameter: _target_path
use_referer: true
remember_me: true
logout: ~
remember_me:
secret: '%secret%'
lifetime: 604800 # 1 week in seconds
path: /
access_control:
- { path: ^/chat/, role: ROLE_CHATTER }
- { path: ^/admin/, role: ROLE_ADMIN }
清除缓存后,这是我的尝试和结果:
- 以
ROL_CHATTER
身份登录:我将按预期http://domain.tld/app_dev.php/chat/
获取登录表单并使用有效凭据 我收到以下消息:拒绝访问。你是 CHATTER。这是正确的,因为我在security.interactive_login
上有一个监听器,这就是当用户使用这些凭据登录时我正在做的事情。 - 以
ROL_ADMIN
身份登录:我将按预期http://domain.tld/app_dev.php/admin/
获取登录表单并使用有效凭据 我收到以下消息:凭据错误.这是错误的,因为凭据是有效的,至少我应该收到另一条消息(拒绝访问。你是管理员),因为security.interactive_login
上的侦听器但正如我所说,这不是什么正在发生。
与监听器相关的信息在
您的问题是用于匹配 global
防火墙请求的正则表达式是 /admin/(.*)|^/chat/(.*)
,但您的检查路径是 /login_check
。如您所见,该路径不会与您的防火墙匹配,从而导致您发布错误消息。
如果我是你,我会简单地删除登录相关内容的防火墙,并将 global
防火墙的正则表达式更改为 /
。然后您只需添加 anonymous: true
以便未登录的用户能够访问登录表单。您的访问控制部分仍会拒绝访问您的保护区。