简体   繁体   English

symfony 中的自动装配 Predis 接口

[英]autowire Predis Interface in symfony

i wanna use ClientInterface in my class constructor and i give an error:我想在我的 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?

seems to be i should add it manually to services.yml i added it like:似乎我应该手动将它添加到 services.yml 我添加它像:

Predis\ClientInterface: '@Predis\Client'

and now i give this error:现在我给出这个错误:

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

what is the solution and why symfony itself dont handle it?什么是解决方案,为什么 symfony 本身不处理它?

you seem to be confused about how to define a service... which isn't surprising tbh您似乎对如何定义服务感到困惑……这并不奇怪

look here看这里

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

for example例如

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

where在哪里

IDevJobService is an INTERFACE and IDevJobService是一个接口和

TestDevJobService is the actual implementation that will be auto injected TestDevJobService是自动注入的实际实现

using @ inside the yaml files is done to reference a service that has already been defined ELSEWHERE在 yaml 文件中使用@来引用已经在 ELSEWHERE 定义的服务

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

you probably want to watch symfonycasts services tutorial (I am not affiliated and I havent watched it myself yet (sure wish I did)).你可能想看 symfonycasts 服务教程(我不是附属的,我自己还没有看过(当然希望我看过))。

EDIT Predis\Client is a 3rd party class.编辑Predis\Client是第 3 方 class。 It isn't in your App namespace or in your src folder.它不在您的App命名空间或src文件夹中。 Symfony checks the src folder for class that it will then make to a service. Symfony 检查 class 的src文件夹,然后它将用于服务。 See services.yaml there is a comment there, look for exclude and resource.services.yaml那里有评论,寻找排除和资源。 And I'm not sure, even if you autoload it, that you can then just do @Predis\Client to reference an existing service.而且我不确定,即使您自动加载它,您是否可以执行@Predis\Client来引用现有服务。

be sure as well to debug your config using一定要使用调试你的配置

php bin/console debug:autowiring

under linux you could do as well php bin/console debug:autowiring | grep Predis在 linux 下你也可以做php bin/console debug:autowiring | grep Predis php bin/console debug:autowiring | grep Predis to find it more quickly (if it is there at all) php bin/console debug:autowiring | grep Predis更快地找到它(如果它存在的话)

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

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