繁体   English   中英

symfony 中的自动装配 Predis 接口

[英]autowire Predis Interface in symfony

我想在我的 class 构造函数中使用 ClientInterface,我给出了一个错误:

Cannot autowire service "App\Service\DelayReportService": argument "$client" of method "__construct()" references interface "Predis\ClientInterface" but no such service exists. Did you create a class that implements this interface?

似乎我应该手动将它添加到 services.yml 我添加它像:

Predis\ClientInterface: '@Predis\Client'

现在我给出这个错误:

You have requested a non-existent service "Predis\Client".

什么是解决方案,为什么 symfony 本身不处理它?

您似乎对如何定义服务感到困惑……这并不奇怪

看这里

https://symfony.com/doc/5.4/service_container.html#explicitly-configuring-services-and-arguments

例如

    services:
        App\Service\Concerns\IDevJobService:
          class: App\Tests\Service\TestDevJobService
          autowire: true
          public: true

在哪里

IDevJobService是一个接口和

TestDevJobService是自动注入的实际实现

在 yaml 文件中使用@来引用已经在 ELSEWHERE 定义的服务

https://symfony.com/doc/5.4/service_container.html#service-parameters

你可能想看 symfonycasts 服务教程(我不是附属的,我自己还没有看过(当然希望我看过))。

编辑Predis\Client是第 3 方 class。 它不在您的App命名空间或src文件夹中。 Symfony 检查 class 的src文件夹,然后它将用于服务。 services.yaml那里有评论,寻找排除和资源。 而且我不确定,即使您自动加载它,您是否可以执行@Predis\Client来引用现有服务。

一定要使用调试你的配置

php bin/console debug:autowiring

在 linux 下你也可以做php bin/console debug:autowiring | grep Predis php bin/console debug:autowiring | grep Predis更快地找到它(如果它存在的话)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM