控制器 "SecurityController::loginAction()" 要求您为“$authenticationUtils”参数提供一个值

Controller "SecurityController::loginAction()" requires that you provide a value for the "$authenticationUtils" argument

自从从我的项目 (symfony3.4) 中删除了 fos 用户包后,我正在尝试设置一个登录表单

我的问题是 loginAction 需要 AuthenticationUtils 但它接收到 null。

我尝试在我的 services.yml 中链接它,但它不会让步。

任何帮助将不胜感激。

这里有以下文件 [SecurityController.php, services.yml, routing.tml]

SecurityController.php

<?php

 namespace AppBundle\Controller;
 
 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
 use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
 
 class SecurityController extends Controller
 {
     public function loginAction(AuthenticationUtils $authenticationUtils)
     {
         // get the login error if there is one
         $error = $authenticationUtils->getLastAuthenticationError();

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

         return $this->render('security/login.html.twig', [
             'last_username' => $lastUsername,
             'error'         => $error,
         ]);
     }
 }

routing.yml

login:
  path: /{_locale}/login
  defaults: { _controller: 'AppBundle:Security:login' }
  requirements:
    _locale: "%languages%"

services.yml

services:      
    AppBundle\Controller\SecurityController:
        class:  'AppBundle\Controller\SecurityController'
        arguments: ['@security.authentication_utils']

希望有人有想法,因为我已经被困了几天了。

提前致谢

如果您使用自动装配,这会更容易(因为您不需要任何复杂的配置即可在操作中使用服务)- 但让我们看看为什么这目前不起作用。

通过您的服务配置,您为 AppBundle\Controller\SecurityController 的构造函数提供了参数。 class 在当前状态下不包含任何构造函数,因此 class 不包含对 AuthenticationUtils 服务的任何引用。

如果你不想使用自动装配,这会有所帮助:向你的控制器添加一个构造函数class,Symfony 的容器将注入服务

class SecurityController extends Controller
 {
     private $authenticationUtils;

     public function __construct(AuthenticationUtils $authenticationUtils) {
         $this->authenticationUtils = $authenticationUtils;
     }

     public function loginAction()
     {
         // get the login error if there is one
         $error = $this-authenticationUtils->getLastAuthenticationError();

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

         return $this->render('security/login.html.twig', [
             'last_username' => $lastUsername,
             'error'         => $error,
         ]);
     }
 }