0
votes

I'm trying to make a Bundle (Symfony 4) for managing users of all our projects and I'm having a problem.

Cannot autowire argument $authenticationUtils of "App\Aroban\Bundle\UtilisateurBundle\Controller\SecurityController::login()": it references class "Symfony\Component\Security\Http\Authentication\AuthenticationUtils" but no such service exists.

I do not understand why the service is not injected...

In the composer.json of the project, there is "symfony/security-bundle": "4.3.*"

In the Bundle:

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]);
    }

.......
}

Configuration.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 (bundle)

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']

When I execute the command

php bin/console debug:container | grep security

I do not see the service ...

Symfony\Component\Security\Csrf\CsrfTokenManagerInterface alias for "security.csrf.token_manager"
Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface alias for "security.csrf.token_generator"
Symfony\Component\Security\Csrf\TokenStorage\TokenStorageInterface alias for "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
// To search for a specific service, re-run this command with a search term. (e.g. debug:container
// log)

Thanks for your help!

1

1 Answers

0
votes

Did you try to composer install afterwards ?

This will install the dependencies you specified in your composer.json

You may have to remove your composer.lock so that it installs your added dependency (see the documentation ). The simplest way may be to use composer require symfony/security-bundle:4.3 instead