(Symfony 4)无法注入我用composer安装的github库

问题描述 投票:2回答:1

我从GitHub安装了以下库:https://github.com/rosell-dk/webp-convert

我需要的主类的位置(来自项目根目录):

\vendor\rosell-dk\webp-convert\src\WebPConvert.php

以下是WebPConvert.php类的启动方式:

namespace WebPConvert;

use WebPConvert\Converters\ConverterHelper;
use WebPConvert\ServeExistingOrConvert;
use WebPConvert\Serve\ServeExistingOrHandOver;

class WebPConvert
{

在我使用它的存储库类中,以下是我尝试进行依赖注入的方法:

use WebPConvert\WebPConvert;

class PhotoRepository extends ServiceEntityRepository
{

    /**
     * @var WebPConvert
     */
    protected $webPConverter;

    public function __construct(WebPConvert $webPConverter)
    {
        $this->webPConverter = $webPConverter;
    }

我按照https://symfony.com/doc/current/service_container.html的指示

但我仍然得到这样的信息:

Cannot autowire service "App\Repository\PhotoRepository": argument "$webPConverter" of method "__construct()" references class "WebPConvert\WebPConvert" but no such service exists.

我甚至尝试将它放在我的services.yaml中,它不起作用:

App\Repository\PhotoRepository:
    arguments:
        - WebPConvert\WebPConvert

我缺少一个额外的步骤吗?

symfony dependencies autowired code-injection
1个回答
2
投票

这是Cerad的答案:

WebPConvert不是Symfony包,因此它不会定义任何服务。您必须手动定义它们。实际上,从自述文件中看起来,WebPConvert :: convert是一个静态方法,所以没有什么可注入的。请按照示例进行操作。

© www.soinside.com 2019 - 2024. All rights reserved.