Symfony 4 AuthenticationUtils

Symfony 4 AuthenticationUtils

问题描述:

我想创建一个基于symfony.com教程的symfony 4用户登录。Symfony 4 AuthenticationUtils

我用下面的代码使用了基本的services.yaml文件。

https://symfony.com/doc/current/security/form_login_setup.html

public function loginAction(Request $request, \Twig_Environment $twigRenderer,AuthenticationUtils $authUtils) 
{ 

    // get the login error if there is one 
    $error = $authUtils->getLastAuthenticationError(); 

    // last username entered by the user 
    $lastUsername = $authUtils->getLastUsername(); 


    return new response($twigRenderer->render('security/login.html.twig', array(
     'last_username' => $lastUsername, 
     'error'   => $error, 
     ) 
    )); 
} 

Errror:

控制器 “应用程序\控制器\ SecurityController ::在loginAction()” 要求您提供的 “$ authUtils” 参数的值。参数是可为空的,并且没有提供空值,没有提供默认值,或者因为在这之后有一个非可选参数。

我不知道如何解决这个问题。有人有建议吗?

+0

我在4.0上遇到了同样的问题,下面的两条建议无效。我也尝试注入构造函数(应该不重要),但结果相同。很奇怪,因为我在测试3.4时遇到了同样的问题,但最终得到了它的工作。这些修补程序是否适用于您@Erik –

我是同样的问题..尝试运行命令:

composer require security 

这些添加到您的services.yml解决这个错误:

AppBundle\Controller\: 
    resource: '../../src/AppBundle/Controller' 
    public: true 
    tags: ['controller.service_arguments'] 

退房这个问题: simple login form using symfony

希望得到这个帮助。

+0

这只是将控制器添加到依赖项注入中。这里的问题是,'AuthenticationUtils'没有被注入。可能是因为安全组件(如@Guhh)建议缺失。 – wawa

我有点晚了,但仍然在这里添加其他引用。

请将autowire: true添加到您的控制器服务声明中。 Symfony会自动为你注入方法参数。