1

我想在自己的服务中自动装配捆绑服务接口,但出现错误:

Cannot resolve argument $slackOauthService of "App\Controller\Panel\Slack\SigninController::afterOauth()": Cannot autowire service "App\Service\Slack\SlackOauthService": argument "$httpClient" of method "__construct()" references interface "GuzzleHttp\ClientInterface" but no such service exists. Did you create a class that implements this interface?

我的代码是:

public function __construct( ClientInterface $httpClient, EntityManagerInterface $em ) {
        $this->httpClient = $httpClient;
        $this->em         = $em;
    }

如果我只注入实体管理器或我定义的服务,一切正常,但无论我尝试注入什么捆绑服务(guzzle、browserkit、jms 序列化程序等),它都不起作用。

我为 Web 项目定义了默认的 symfony services.yaml 文件:

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.

# 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']

我正在使用 Symfony4.2。

这个问题的解决方案是什么?

4

1 回答 1

2

我之前玩过这个。您是否导入了 GuzzleHttp 包?如果是这样,也许这会对您有所帮助:

use GuzzleHttp\Client;

public function __construct()
{
   $this->http_client = new Client();
}
于 2019-04-23T09:17:47.970 回答