我想在类构造函数中使用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本身不处理它?
发布于 2022-08-30 13:35:49
你似乎对如何定义服务感到困惑.这并不奇怪
看这儿
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文件中使用@来引用已经在其他地方定义的服务
https://symfony.com/doc/5.4/service_container.html#service-parameters
您可能想看symfonycasts服务教程(我不是附属的,我还没有亲自看过它(当然希望我看了)。
编辑 Predis\Client是一个第三方类。它不在App名称空间或src文件夹中。Symfony检查src文件夹中的类,然后将其创建到服务中。参见services.yaml那里有一个注释,查找排除和资源。而且我也不确定,即使您自动加载了它,您也可以只执行@Predis\Client来引用现有的服务。
也要确保使用
php bin/console debug:autowiring
在linux下,您也可以通过php bin/console debug:autowiring | grep Predis来更快地找到它(如果它存在的话)
https://stackoverflow.com/questions/73542639
复制相似问题