我想在我的類建構式中使用 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 本身不處理它?
uj5u.com熱心網友回復:
您似乎對如何定義服務感到困惑……這并不奇怪
看這里
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 服務教程(我不是附屬的,我自己還沒有看過(當然希望我看過))。
EDIT
Predis\Client是第 3 方課程。它不在您的App命名空間或src檔案夾中。Symfony 檢查src檔案夾中的類,然后它會為服務創建類。看到services.yaml那里有評論,尋找排除和資源。而且我不確定,即使您自動加載它,您是否可以這樣做@Predis\Client來參考現有服務。
一定要使用除錯你的配置
php bin/console debug:autowiring
在linux下你也可以php bin/console debug:autowiring | grep Predis更快地找到它(如果它在那里的話)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/504745.html
下一篇:Vue筆記
