当我尝试在控制器操作中使用@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 AppController;
use AppDocumentCompany;
use AppServiceDocumentManagerCompanyManager;
use FOSRestBundleControllerFOSRestController;
use SensioBundleFrameworkExtraBundleConfigurationMethod;
use SensioBundleFrameworkExtraBundleConfigurationParamConverter;
use SwaggerAnnotations as SWG;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentRoutingAnnotationRoute;
/**
* @Route("/profile")
*/
class ProfileController extends FOSRestController
{
/**
* @Route("/top/{id}")
* @Method("GET")
* @SWGResponse(
* response=200,
* description="Returns top profiles",
* )
* @SWGTag(name="profile")
*
* @ParamConverter("company", class="AppDocumentCompany")
* @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}'
AppController:
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: SensioBundleFrameworkExtraBundleRequestParamConverterDoctrineParamConverter
tags:
- { name: request.param_converter, converter: doctrine.odm }
arguments: ['@doctrine_mongodb']
那么@ParamConverter
现在应该可以正常工作了。