【问题标题】:UserPasswordEncoderInterface Autowiring Not Working Symfony 4.4UserPasswordEncoderInterface 自动装配不起作用 Symfony 4.4
【发布时间】:2020-09-22 20:05:03
【问题描述】:

我有一个全新安装的 symfony 4.4 的超级基本 API 端点,我收到以下错误:

无法自动装配参数 $passwordEncoder 的 “App\Controller\AuthenticationController::authenticateAction()”:它 参考界面 “Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface” 但不存在这样的服务。

我的控制器:

<?php

namespace App\Controller;

use App\Entity\User;
use FOS\RestBundle\Controller\AbstractFOSRestController;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\Annotations\Route;
use Lexik\Bundle\JWTAuthenticationBundle\Encoder\JWTEncoderInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;

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

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

如果我删除UserPasswordEncoderInterface $passwordEncoder,我会得到一个成功的结果(现在预计)。我的User 实体没有什么特别之处,并且正确地扩展了UserInterface

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
    App\Controller\:
        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

使用 Symfony 4.4 和 php 7.2.20

几乎可以肯定这是某种配置问题,但我并没有遵循我做错的事情。

【问题讨论】:

    标签: php symfony4


    【解决方案1】:

    我聪明吗,这是配置问题!

    我的security.yaml 文件位于/config 主目录中,而不是/config/packages 目录中。好吧,也许我没那么聪明……

    不确定它是如何到达那里的。我认为一些过时的包在config/packages 目录中找不到它。

    我的生命有 48 小时......

    【讨论】:

    • 您是否手动创建了security.yaml 文件? O.o
    • 不,它是在我运行composer require symfony/security-bundle 时自动生成的。就像我说的,它一定是一些过时的包,不喜欢它的位置或其他东西。不知道为什么我会移动它。 :耸肩:
    猜你喜欢
    • 2019-01-18
    • 2021-04-27
    • 1970-01-01
    • 2020-03-08
    • 2014-09-01
    • 2017-01-19
    • 1970-01-01
    • 1970-01-01
    • 2013-07-14
    相关资源
    最近更新 更多