交响乐4。 ParamConverter 注释通过自动装配注入服务时发生冲突

Symfony4. ParamConverter annotation conflicts injecting a service by autowire

当我尝试在 Controller 操作中使用 @ParamConverter 注释时,出现错误

"Cannot resolve argument $company of \"App\Controller\ProfileController::top()\": Cannot autowire service \".service_locator.0CrkHeS\": it references class \"App\Document\Company\" but no such service exists."

我知道这样的服务不存在,因为我在 services.yaml 中排除了 Document 路径。我只需要从 Repostiroy 中找到一个公司文档对象。

这是我的控制器代码:

<?php

// src/Controller/ProfileController.php
namespace App\Controller;

use App\Document\Company;
use App\Service\DocumentManager\CompanyManager;
use FOS\RestBundle\Controller\FOSRestController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Swagger\Annotations as SWG;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @Route("/profile")
 */
class ProfileController extends FOSRestController
{
    /**
     * @Route("/top/{id}")
     * @Method("GET")
     * @SWG\Response(
     *     response=200,
     *     description="Returns top profiles",
     * )
     * @SWG\Tag(name="profile")
     *
     * @ParamConverter("company", class="App\Document\Company")
     * @param CompanyManager $companyManager
     * @return Response
     */
    public function top(CompanyManager $companyManager, Company $company)
    {
        dump($company->getId());exit;
        return $this->handleView($this->view($companyManager->getTopProfiles(), Response::HTTP_OK));
    }

}

services.yaml配置:

services:
    _defaults:
        autowire: true
        autoconfigure: true
        public: false

    App\:
        resource: '../src/*'
        exclude: '../src/{Entity,Document,Migrations,Tests,Kernel.php,Exception,DataFixtures}'

    App\Controller\:
        resource: '../src/Controller'
        tags: ['controller.service_arguments']

以防其他人遇到同样的问题。 该问题与自动装配冲突无关。 @ParamConverter 没用,因为我用的是 mongoDB 和 Doctrine ODM,不是 ORM。默认情况下,Doctrine ParamConverter 不适用于 mongo 文档。 所以我在这里找到了一些信息 https://matthiasnoback.nl/2012/10/symfony2-mongodb-odm-adding-the-missing-paramconverter/

services.yaml 文件中定义一个新服务:

doctrine_mongo_db_param_converter:
    class: Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\DoctrineParamConverter
    tags:
        - { name: request.param_converter, converter: doctrine.odm }
    arguments: ['@doctrine_mongodb']

那么 @ParamConverter 现在应该可以正常工作了。