问题描述
我必须在我的服务页面使用 HttpClientInterface
。所以我定义如下:
use Symfony\Contracts\HttpClient\HttpClientInterface;
public function __construct(HttpClientInterface $httpClient)
{
$this->httpClient = $httpClient;
}
在我的 services.xml
文件中,我也尝试传递参数并设置 autowiring
<!-- SERVICES -->
<service id="SwagMasterBundle\Service\ProductManager" autowire="true" autoconfigure="true">
<argument id="Symfony\Contracts\HttpClient\HttpClientInterface" type="service" key="$httpClient"></argument>
</service>
但我收到错误:
The service "SwagMasterBundle\Service\ProductManager" has a dependency on a non-existent service "Symfony\Contracts\HttpClient\HttpClientInterface".
有人能帮我做错什么吗?
谢谢。
解决方法
你不能期待一个接口,因为一个接口需要从一个具体的类中实现。因此,在您的情况下,您需要一个具体的对象 - Symfony 服务是简单的对象。
请先参考面向对象编程基础。
编辑:自动装配和定义给定的服务也没有意义。让框架决定需要的对象或自己做,但选择一个选项,而不是一次两个。