如何解决"There is no extension able to load the configuration for 'monolog.http_client' "

How to solve "There is no extension able to load the configuration for 'monolog.http_client' "

我想使用 symfony 的 HttpClientInterface,所以我在 __construct 中添加了它,如下所示:

private HttpClientInterface $client;

/**
* @param HttpClientInterface $client
*/
public function __construct(
   HttpClientInterface $client
) {
    $this->client = $client;
}

在我的函数中只是一个简单的请求:

$response = $this->client->request(
  'GET',
  'https://mywebsite.com/api/call/myId'
);

所以当我尝试 运行 时,我得到了这个错误:

Cannot autowire service "App\MyService": argument "$client" of method "__construct()" references interface "Symfony\Contracts\HttpClient\HttpClientInterface" but no such service exists. You should maybe alias this interface to the existing "monolog.http_client" service.

我尝试按照上面所说的将它添加到服务中,但我也遇到了错误:

像这样(monolog.http_client: 或 http_client:)

错误:这里没有能够加载“monolog.http_client”配置的扩展程序

所以我真的不知道如何解决这个问题!!

该组件可能不在您的供应商中。 尝试 php bin/console debug:container httpClient 如果找不到服务,请使用 composer require "symfony/http-client"

安装

我不得不添加 http_client: under framework: in services.yml