skip to Main Content

So I have a very simple service for now. This is my services.yaml:

# This file is the entry point to configure your own services.
# Files in the packages/ subdirectory configure your dependencies.

# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration
parameters:

imports:
    - resource: myservice.yaml

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.

    # makes classes in src/ available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    App:
        resource: '../src/'
        exclude:
            - '../src/DependencyInjection/'
            - '../src/Entity/'
            - '../src/Kernel.php'

    # add more service definitions when explicit configuration is needed
    # please note that last definitions always *replace* previous ones

and the file myservice.yaml is:

services:
 service.http_client:
    class: GuzzleHttpClient
    public: true
    arguments:
      -
        timeout: 2.0
        base_uri: "%env(BASE_URI)%"
        handler: GuzzleHttpHandlerStack
        headers:
          X-Api-Key: "%env(API_KEY)%"

  service.service_client:
    class: ServiceV1alphaServiceClient
    arguments:
      - '%env(BASE_URI)%'
      - '@deeplink.http_client'
      - ~
      - ~
      - ''

  service:
    class: AppServiceService
    public: true
    arguments:
      - "@service.service_client"

when I try to use it in my Controller like:

    public function __construct(private readonly Service $deeplinkService)
    {}

It constantly fails saying that ServiceV1alphaServiceClient no such service exists. So this service clearly exists in ma vendor folder and the namespace is correct. What is the issue with autowiring here?

2

Answers


  1. Chosen as BEST ANSWER

    I was able to solve it. This was the relevant fix. I needed an alias:

    https://stackoverflow.com/a/50952060/11872619

    Props to Sam, you the real one!


  2. I see few things that are not necessary:

    services:
      ServiceV1alphaServiceClient:
        arguments:
          $uri: '%env(BASE_URI)%'
          $ĥttpClient: '@deeplink.http_client'
    

    And your class doesn’t seems to be declared in your services.yaml (it is already done in App: and _defaults), the container will be there for you.

    public function __construct(private readonly ServiceClient $deeplinkService)
    {}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search