1

我遇到了这个相当常见的 Symfony 登录失败并出现此错误消息的问题:

Unable to find the controller for path "/auth/login_check".

这个问题的常见答案是login_check路由不在防火墙后面,但在我的情况下它是!这是我的配置文件:

应用程序/配置/security.yml

firewalls:
    dev:
        pattern:    ^/(_(profiler|wdt)|css|images|js)/
        security:   false

    auth_area:
        pattern:    ^/auth/(login|login_check)
        anonymous:  ~

    secured_area:
        pattern:    ^/
        form_login:
            login_path: /auth/login
            check_path: /auth/login_check
        logout:
            path:   /auth/logout
            target: /

应用程序/配置/路由.yml

login:
    pattern: /auth/login
    defaults: { _controller: AuthBundle:Default:login }

login_check:
    pattern: /auth/login_check

logout:
    pattern: /auth/logout

AuthBundle/Controller/DefaultController.php

public function loginAction()
{
  $form = $this->createForm(new UserType(), new User());

  return $this->render('AuthBundle:Default:login.html.twig', array(
    'form' => $form->createView(),
    'action' => $this->generateUrl('login_check'),
  ));
}

AuthBundle/Form/Type/UserType.php

public function buildForm(FormBuilderInterface $builder, array $options)
{
  $builder->add('username', 'text', array('label' => 'Login:'));
  $builder->add('password', 'password', array('label' => 'Password:'));
}

AuthBundle/Resources/views/Default/login.html.twig

...
<form action="{{ action }}" method="POST">
  {{ form_widget(form) }}
  <input type="submit" value="Login" />
</form>
...

我的表单是使用路由login_check作为目标(/auth/login_checkURL)创建的。

secured_area防火墙应该匹配所有 URL,所以/auth/login_check应该在其中。然而,我不断收到这个错误。我究竟做错了什么?

4

3 回答 3

2

login_check url 必须位于与使用防火墙相同的防火墙后面。请改用 ACL。

firewalls:
    dev:
        pattern:    ^/(_(profiler|wdt)|css|images|js)/
        security:   false

    secured_area:
        pattern:    ^/
        form_login:
            login_path: /auth/login
            check_path: /auth/login_check
        logout:
            path:   /auth/logout
            target: /
        anonymous:  ~

access_control:
    - { path: ^/auth/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/, role: ROLE_USER }
于 2013-08-05T14:32:15.723 回答
0

应用程序/配置/security.yml

secured_area:
    pattern:    ^/
    form_login:
        login_path: login  #this is routing name
        check_path: login_check  #this is routing name
    logout:
        path:   logout #this is routing name
        target: /

编辑

[1]你可以尝试删除这个:

auth_area:
    pattern:    ^/auth/(login|login_check)
    anonymous:  ~

[2]你的路径更好:

 <form action="{{ path('login_check') }}" method="POST">

编辑

这是登录的示例

firewalls:
    dev:
        pattern:  ^/(_(profiler|wdt)|css|images|js)/
        security: false
    secured_area:
        pattern:    ^/
        anonymous: ~
        form_login:
            login_path:  login
            check_path:  login_check
            always_use_default_target_path: true
            default_target_path: /top
        logout:
            path:   /user_logout
            target: /
于 2013-08-05T13:37:31.063 回答
0

路由.yml

login_check:
    pattern:  /login_check
    defaults: { _controller: AcmeUserBundle:Default:loginCheck }

DefaultController.php

public function loginCheckAction()
{
    return $this->redirect($this->generateUrl('login'));
}
于 2013-11-04T14:28:15.350 回答