【问题标题】:Symfony 4 Bundle: AuthenticationUtils but no such service existsSymfony 4 Bundle:AuthenticationUtils 但不存在这样的服务
【发布时间】:2020-03-09 03:09:57
【问题描述】:

我正在尝试制作一个 Bundle (Symfony 4) 来管理我们所有项目的用户,但遇到了问题。

无法自动装配“App\Aroban\Bundle\UtilisateurBundle\Controller\SecurityController::login()”的参数 $authenticationUtils:它引用了类“Symfony\Component\Security\Http\Authentication\AuthenticationUtils”,但不存在此类服务。

不明白为什么不注入服务...

在项目的composer.json中有“symfony/security-bundle”:“4.3.*”

在捆绑包中:

SecurityController.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/console debug:container | grep 安全性

我没有看到服务...

“security.csrf.token_manager”的 Symfony\Component\Security\Csrf\CsrfTokenManagerInterface 别名
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
// 要搜索特定服务,请使用搜索词重新运行此命令。 (例如调试:容器
// 日志)

感谢您的帮助!

【问题讨论】:

    标签: php service dependency-injection bundle symfony4


    【解决方案1】:

    之后你有没有尝试composer install

    这将安装您在 composer.json 中指定的依赖项

    您可能必须删除您的 composer.lock 以便它安装您添加的依赖项(请参阅 documentation )。最简单的方法可能是使用 composer require symfony/security-bundle:4.3 代替

    【讨论】:

    • 您可能必须删除您的 composer.lock 以便它安装您添加的依赖项(请参阅 getcomposer.org/doc/… )最简单的方法可能是改用 composer require symfony/security-bundle:4.3
    • 您可以编辑您的答案以将此信息放入答案本身。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-23
    • 2020-03-21
    • 2018-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多