Symfony 3.1:无法找到路径“/logout”的控制器

Symfony 3.1: Unable to find the controller for path "/logout"

我正在尝试使用 Symfony 3.1 完成我的注销功能,但到目前为止它还没有工作。我正在一步一步地遵循这本书的文档,但我得到的只是一个未找到的异常:

Unable to find the controller for path "/logout". The route is wrongly configured.

我在 security.yml 文件中激活了正确的配置参数(注销)

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。如果您没有任何 bundle/controller 来处理该路由,则您必须实施一个。

我认为这是因为您定义了两个防火墙。目前,摆脱 secured_area 东西并尝试类似的东西:

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

请注意,注销部分位于主防火墙之下。一旦你的主防火墙开始工作,那么如果你真的需要它,你可以尝试重新添加 secured_area。

是的,我很懒惰,只是 copied/pasted 一个工作配置。您必须调整路线以匹配您的路线。

为 Cerad 的回答添加更多细节:Symfony 中的安全用户绑定到 "security context",默认情况下,它对应于防火墙。在您的情况下,您希望注销已强加 form_login 身份验证要求的上下文 ("firewall"),因此需要在 "main" 防火墙上设置注销配置,而不是一个名为 "secured_area".

的新

文档使用防火墙名称 "secured_area" 只是为了表明此配置旨在用于保护您网站的一部分的防火墙中。您无意将该名称逐字复制到您的配置中;相反,在您的情况下,您要保护的防火墙称为 "main"。从这个意义上说,documentation 有点令人困惑,因为它在其他任何地方都只使用名为 "main" 的防火墙作为示例,包括设置安全区域的示例。所以我想如果在这个特定的例子中使用 "main" 可能会更好。

此外,您需要确保注销路由(路径)实际由您为其配置的防火墙处理,而不是 "caught" 由其他防火墙处理。在你的情况下,这会自动发生,因为你没有对你的 "main" 防火墙施加任何路径限制,所以它会捕获所有内容。

如果您想使用两个防火墙,您可能还需要两个登录表单和两个注销路由。

添加连接路由很重要 "pattern" 每个防火墙都能识别

路由配置:

# 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

感谢最正确的回答。还有一个技巧你应该知道: 确保 /logout 在防火墙后面。 /logout 路径必须匹配防火墙模式。

例如:'pattern: ^/admin' 那么注销路径应该是'/admin/logout'。