php - Symfony 4 Bundle:AuthenticationUtils 但不存在这样的服务
问题描述
我正在尝试制作一个 Bundle (Symfony 4) 来管理我们所有项目的用户,但我遇到了问题。
无法自动装配“App\Aroban\Bundle\UtilisateurBundle\Controller\SecurityController::login()”的参数 $authenticationUtils:它引用了类“Symfony\Component\Security\Http\Authentication\AuthenticationUtils”,但不存在此类服务。
我不明白为什么没有注入服务......
在项目的composer.json中有“symfony/security-bundle”:“4.3.*”
在捆绑包中:
安全控制器.php
<?php
namespace App\Aroban\Bundle\UtilisateurBundle\Controller;
use App\Aroban\Bundle\UtilisateurBundle\Entity\Utilisateur;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Swift_Mailer;
class SecurityController extends AbstractController
{
public function login(AuthenticationUtils $authenticationUtils): Response
{
$error = $authenticationUtils->getLastAuthenticationError();
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('@Utilisateur/security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
}
.......
}
配置.php
<?php
namespace App\Aroban\Bundle\UtilisateurBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder('utilisateur');
$rootNode = $treeBuilder->getRootNode();
return $treeBuilder;
}
}
UtilisateurExtension.php
<?php
namespace App\Aroban\Bundle\UtilisateurBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader;
class UtilisateurExtension extends Extension
{
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container): void
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.yaml');
}
}
services.yaml(捆绑包)
services:
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
public: false # Allows optimizing the container by removing unused services; this also means
App\Aroban\Bundle\UtilisateurBundle\:
resource: '../../*'
exclude: '../../{Entity,Migrations,Tests,Kernel.php}'
App\Aroban\Bundle\UtilisateurBundle\Controller\:
resource: '../../Controller/*'
tags: ['controller.service_arguments']
当我执行命令时
php bin/控制台调试:容器 | grep 安全性
我没有看到服务...
Symfony\Component\Security\Csrf\CsrfTokenManagerInterface 别名为“security.csrf.token_manager”
Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface 别名为“security.csrf.token_generator”
Symfony\Component\Security\Csrf\TokenStorage\TokenStorageInterface 别名对于“security.csrf.token_storage”
教义.orm.security.user.provider Symfony\Bridge\Doctrine\Security\User\EntityUserProvider
maker.security_config_updater Symfony\Bundle\MakerBundle\Security\SecurityConfigUpdater
security.csrf.token_generator Symfony\Component\Security\Csrf\TokenGenerator\UriSafeTokenGenerator
security.csrf.token_manager Symfony\Component\Security\Csrf\CsrfTokenManager
security.csrf.token_storage Symfony\Component\Security\Csrf\TokenStorage\SessionTokenStorage
twig.extension. security_csrf Symfony\Bridge\Twig\Extension\CsrfExtension
twig.runtime.security_csrf Symfony\Bridge\Twig\Extension\CsrfRuntime
// 要搜索特定服务,请使用搜索词重新运行此命令。(例如调试:容器
//日志)
谢谢你的帮助!
解决方案
你composer install
后来试过了吗?
这将安装您在 composer.json 中指定的依赖项
您可能必须删除您的 composer.lock 以便它安装您添加的依赖项(请参阅文档)。最简单的方法可能是使用 composer require symfony/security-bundle:4.3 代替
推荐阅读
- c# - 为什么我不能在这个控制器类中使用 BaseController Initialize() 方法?
- angular - 与 ReactiveForms 内联添加的 Angular 异步验证器在 new 时触发
- python - 将python脚本导入其他并同时运行
- reporting-services - 从 Power BI 传递参数运行 SSRS 报告
- linux - jupyter lab - 抑制控制台输出
- php - Microsoft Graph api:订阅通知请求失败问题
- javascript - 尝试使用 javascript 删除 DOM 表
- hadoop - 为什么hadoop对reducer的输入进行排序?
- 2sxc - 在当前版本 9.33 中,在 Head 中包含 CSS 似乎不起作用
- javascript - 如何在使用 Ajax 成功单击提交按钮后隐藏模式?