2

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: GuzzleHttp\Client
    public: true
    arguments:
      -
        timeout: 2.0
        base_uri: "%env(BASE_URI)%"
        handler: GuzzleHttp\HandlerStack
        headers:
          X-Api-Key: "%env(API_KEY)%"

  service.service_client:
    class: Service\V1alpha\ServiceClient
    arguments:
      - '%env(BASE_URI)%'
      - '@deeplink.http_client'
      - ~
      - ~
      - ''

  service:
    class: App\Service\Service
    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 Service\V1alpha\ServiceClient 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?

vukojevicf
  • 609
  • 1
  • 4
  • 22
  • `Service` and `ServiceClient` are different classes - why do you want to autowire one to the other? And how is ths a problem related to Composer? – Nico Haase Aug 03 '23 at 10:00
  • Does this answer your question? [Cannot autowire service](https://stackoverflow.com/questions/49307819/cannot-autowire-service) – A.L Aug 05 '23 at 17:00

1 Answers1

4

I see few things that are not necessary:

services:
  Service\V1alpha\ServiceClient:
    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)
{}
Roukmoute
  • 681
  • 1
  • 11
  • 26