Skip to content
Advertisement

UserPasswordEncoderInterface Autowiring Not Working Symfony 4.4

I have a super basic API endpoint with a fresh install of symfony 4.4 and I’m getting the following error:

Cannot autowire argument $passwordEncoder of “AppControllerAuthenticationController::authenticateAction()”: it references interface “SymfonyComponentSecurityCoreEncoderUserPasswordEncoderInterface” but no such service exists.

My Controller:

<?php

namespace AppController;

use AppEntityUser;
use FOSRestBundleControllerAbstractFOSRestController;
use FOSRestBundleControllerAnnotations as Rest;
use FOSRestBundleControllerAnnotationsRoute;
use LexikBundleJWTAuthenticationBundleEncoderJWTEncoderInterface;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentSecurityCoreEncoderUserPasswordEncoderInterface;
use SymfonyComponentSecurityCoreExceptionCustomUserMessageAuthenticationException;

/**
 * Class AuthenticationController
 *
 * @package AppController
 * @Route("/api/authentication")
 */
class AuthenticationController extends AbstractFOSRestController {

    /**
     * @RestGet("/authenticate")
     *
     * @param Request                      $request
     * @param UserPasswordEncoderInterface $passwordEncoder
     * @param JWTEncoderInterface          $JWTEncoder
     *
     * @return Response
     */
    public function authenticateAction (Request $request, UserPasswordEncoderInterface $passwordEncoder, JWTEncoderInterface $JWTEncoder) {
        exit;
    }
}

If I remove UserPasswordEncoderInterface $passwordEncoder I get a successful nothing (expected for now). My User Entity is nothing special, and extends UserInterface correctly.

services.yaml

# This file is the entry point to configure your own services.
# Files in the packages/ subdirectory configure your dependencies.

# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.

    # makes classes in src/ available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    App:
        resource: '../src/'
        exclude:
            - '../src/DependencyInjection/'
            - '../src/Entity/'
            - '../src/Kernel.php'
            - '../src/Tests/'

    # controllers are imported separately to make sure services can be injected
    # as action arguments even if you don't extend any base controller class
    AppController:
        resource: '../src/Controller/'
        tags: ['controller.service_arguments']

    # add more service definitions when explicit configuration is needed
    # please note that last definitions always *replace* previous ones

Using Symfony 4.4 and php 7.2.20

Almost certain this is some sort of configuration issue, but I’m not following what I did wrong.

Advertisement

Answer

Man am I smart, it was a config issue!

My security.yaml file was in main /config directory and not in the /config/packages directory. Ok maybe I’m not that smart…

Not sure how it got there. I think some out-dated package couldn’t find it in the config/packages directory.

There goes 48 hours of my life…

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement