Php Symfony 3.1:找不到路径“的控制器/注销“;
我正在尝试使用Symfony 3.1完成注销功能,但到目前为止还不能正常工作。我正在一步一步地阅读书籍文档,但我得到的只是一个未发现的异常: 找不到路径“/注销”的控制器。路线错了 配置 我确实在security.yml文件中激活了正确的配置参数(logout)Php Symfony 3.1:找不到路径“的控制器/注销“;,php,symfony,logout,Php,Symfony,Logout,我正在尝试使用Symfony 3.1完成注销功能,但到目前为止还不能正常工作。我正在一步一步地阅读书籍文档,但我得到的只是一个未发现的异常: 找不到路径“/注销”的控制器。路线错了 配置 我确实在security.yml文件中激活了正确的配置参数(logout) security: firewalls: # disables authentication for assets and the profiler, adapt it according to your
security:
firewalls:
# disables authentication for assets and the profiler, adapt it according to your needs
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
anonymous: ~
# activate different ways to authenticate
form_login:
login_path: login
check_path: login
secured_area:
anonymous: ~
logout:
path: /logout
target: /
我确实在routing.yml中创建了一个路由:
logout:
path: /logout
根据文档,不需要控制器,但例外情况是控制器路径错误
我做错了什么?因为您很可能没有与该路线关联的控制器。
如果您使用的是FOSUser之类的捆绑包,则只需导入该捆绑包提供的routing.yml。如果您没有任何包/控制器来管理该路由,则必须实现一个。我认为这是因为您定义了两个防火墙。目前,摆脱安全区域的东西,尝试以下方法:
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
pattern: ^/
anonymous: ~
switch_user: true
form_login:
provider: user_provider
login_path: user_login
check_path: user_login_check
default_target_path: app_welcome
username_parameter: username
password_parameter: password
csrf_parameter: _csrf_token
csrf_token_id: authenticate
csrf_token_generator: security.csrf.token_manager
logout:
path: user_logout
target: app_welcome
请注意,注销部分位于主防火墙下。一旦主防火墙正常工作,如果您确实需要,您可以尝试重新添加安全区域
是的,我很懒,只是复制/粘贴了一个工作配置。您必须调整路由以匹配您的路由。为Cerad的答案添加更多细节:Symfony中的安全用户绑定到“安全上下文”,默认情况下,该上下文对应于防火墙。在您的情况下,您希望注销您已对其施加form_登录身份验证要求的上下文(“防火墙”),因此您的注销配置需要设置在“主”防火墙上,而不是在一个名为“secured_area”的新防火墙上 本文档使用防火墙名称“secured_area”仅作为一种方式,表明此配置旨在用于保护网站一部分的防火墙。并不是要你把这个名字直接复制到你的配置中;相反,在您的情况下,您试图保护的防火墙称为“主防火墙”。从这个意义上讲,有点令人困惑,因为在其他地方,它只使用一个称为“main”的防火墙作为示例,包括设置安全区域的示例。所以我想在这个特定的例子中使用“main”可能会更好
此外,您需要确保注销路由(路径)实际上由您为其配置的防火墙处理,而不是被其他防火墙“捕获”。在您的情况下,这将自动发生,因为您没有对“主”防火墙施加任何路径限制,因此它会捕获所有内容。如果您想使用两个防火墙,您可能还需要两个具有两个注销路由的登录表单 在每个防火墙都能识别的“模式”中添加断开连接路由是很重要的 路由配置:
# config/routes.yaml
logout_medical:
path: /logout-medical
logout_patient:
path: /logout-patient
# I omitted the declaration of the connection routes because they are made with annotations in the controllers
# config/packages/security.yaml
security:
# ...
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: ^/(logout-medical|login-medical|m)
anonymous: ~
provider: medical_provider
form_login:
login_path: login_medical
check_path: login_medical
logout:
path: logout_medical
target: login_medical
patient_area:
pattern: ^/(logout-patient|login-patient|p)
anonymous: ~
provider: patient_provider
form_login:
login_path: login_patient
check_path: login_patient
logout:
path: logout_patient
target: login_patient
防火墙配置:
# config/routes.yaml
logout_medical:
path: /logout-medical
logout_patient:
path: /logout-patient
# I omitted the declaration of the connection routes because they are made with annotations in the controllers
# config/packages/security.yaml
security:
# ...
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: ^/(logout-medical|login-medical|m)
anonymous: ~
provider: medical_provider
form_login:
login_path: login_medical
check_path: login_medical
logout:
path: logout_medical
target: login_medical
patient_area:
pattern: ^/(logout-patient|login-patient|p)
anonymous: ~
provider: patient_provider
form_login:
login_path: login_patient
check_path: login_patient
logout:
path: logout_patient
target: login_patient
谢谢你的最佳答案,这是正确的。您还应该注意另一个技巧: 确保/注销位于防火墙后面/注销路径必须与防火墙模式匹配。
例如:“pattern:^/admin”那么注销路径应该是“/admin/logout”。事实上,我没有与该路径关联的控制器,因为食谱上说不需要(控制器)。经过几次更改(我删除了csrf内容和提供程序),它就工作了!但为什么在文档中使用安全区域?谢谢我能理解这种混乱。配置安全组件可能具有挑战性。而且文档不是100%一致的。考虑对文档工作的贡献: