繁体   English   中英

Symfony4。 ParamConverter注释与通过autowire注入服务冲突

[英]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 path。 我只需要从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']

万一其他人会遇到同样的问题。 这个问题与autowire冲突无关。 @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现在应该可以正常工作了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM