首页 > 解决方案 > 传递给服务的参数在 shopware 6 中给出依赖不存在服务错误

问题描述

我必须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".

任何人都可以帮助我在这里做错了什么吗?

谢谢你。

标签: symfony4shopware

解决方案


你不能指望一个接口,因为一个接口需要从一个具体的类中实现。所以,在你的情况下,你需要一个具体的对象——Symfony 服务是简单的对象。

请先参考面向对象的编程基础。

编辑:自动装配定义给定的服务也没有意义。让框架确定所需的对象或自己做,但选择一个选项,而不是一次两个。


推荐阅读