繁体   English   中英

Symfony 4 捆绑包:AuthenticationUtils 但不存在此类服务

[英]Symfony 4 Bundle: AuthenticationUtils but no such service exists

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

无法自动装配“App\Aroban\Bundle\UtilisateurBundle\Controller\SecurityController::login()”的参数 $authenticationUtils:它引用了 class “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”的别名
doctrine.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 代替

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM