您请求了一项不存在的服务 "fos_user.manager"

You have requested a non-existent service "fos_user.manager"

我使用 symfony 4.1.13 和 FOSUser v2.1.2 当我尝试清除缓存时出现此错误。

您请求了一项不存在的服务"fos_user.manager"。

这是我的控制器函数

 /**
 * Create New User
 * @Rest\Post("/user/create")
 * @param Request $request
 * @param UserManager $userManager
 * @return View
 */
public function createAction(Request $request, UserManager $userManager): View
{
    [...]

    $userManager->updatePassword($user);

    [...]

    return View::create($user, Response::HTTP_OK);
}

这项服务应该存在:

bin/console debug:container
FOS\UserBundle\Model\UserManager  alias for "fos_user.manager"  

这里是config/packages/fos_user.yaml

的内容
# FOSUser
fos_user:
  db_driver: orm
  firewall_name: main
  user_class: App\Entity\User
  from_email:
    address: "noreply@yourcompany.com"
    sender_name: "No Reply"

Service.yaml

parameters:
    locale: 'en'

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.
        public: false       # Allows optimizing the container by removing unused services; this also means
                            # fetching services directly from the container via $container->get() won't work.
                            # The best practice is to be explicit about your dependencies anyway.

    # 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,Entity,Migrations,Tests,Kernel.php}'

    # 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

    sensio_framework_extra.view.listener:
        alias: Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener

    FOS\OAuthServerBundle\Model\AccessTokenManagerInterface:
        alias: fos_oauth_server.access_token_manager.default

    FOS\UserBundle\Model\UserManager:
        alias: fos_user.manager

我通过创建一个服务(User Service)并注入UserManager来解决问题

service.yaml

  App\Service\UserService:
    arguments:
      $userManager: '@fos_user.user_manager'

UserService.php

public function __construct(UserManager $userManager) {
...