Home > OS >  autowire Predis Interface in symfony
autowire Predis Interface in symfony

Time:09-04

i wanna use ClientInterface in my class constructor and i give an error :

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 :

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?

CodePudding user response:

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

for example

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

where

IDevJobService is an INTERFACE and

TestDevJobService is the actual implementation that will be auto injected

using @ inside the yaml files is done to reference a service that has already been defined ELSEWHERE

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)).

EDIT Predis\Client is a 3rd party class. It isn't in your App namespace or in your src folder. Symfony checks the src folder for class that it will then make to a service. See services.yaml there is a comment there, look for exclude and resource. And I'm not sure, even if you autoload it, that you can then just do @Predis\Client to reference an existing service.

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 to find it more quickly (if it is there at all)

  • Related