当我尝试在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."
我知道这样的服务不存在,因为我已经排除了Document
中的services.yaml
路径。我只需要从Repostiroy找到一个Company文档对象。
这是我的控制器代码:
<?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']
答案 0 :(得分:2)
以防其他人遇到相同的问题。
该问题与自动接线冲突无关。 @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
现在应该可以正常工作了。