您已请求不存在的服务“ fos_user.manager”

时间:2019-10-14 16:07:59

标签: symfony4 fosuserbundle

我使用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

1 个答案:

答案 0 :(得分:0)

我通过创建服务(用户服务)并注入UserManager来解决问题

service.yaml

If ( (Get-ADUser -Filter { displayName -eq $user}) -AND ($Returner -eq "Yes")) { 
    $Check = Get-ADUser -Filter{displayName -eq $user} -Properties SamAccountName
    $sam = $check.SamAccountName
    $sam
}

UserService.php

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