Vincent PHILIPPE
Vincent PHILIPPE

Reputation: 1191

Cannot autowire service ... Though service parameter is configure

I have a problem autowiring some parameters in services config.

I can't clear:cache because it triggers the error so I don't think it is a cache issue. And I have copy/paste all my files into a whole new Symfony project and still got the same problem.

This is my services.yaml

#config/services.yaml
parameters:
    app.medipim_api_key_value: '%env(resolve:MEDIPIM_API_KEY)%'
    app.medipim_api_key_id: 326
services:
    [6 lines ...]
    # 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,Entity,Migrations,Tests,Kernel.php}'
    App\Services\MedipimApi:
        arguments:
            $medipim_api_key_id: '%app.medipim_api_key_id%'
            $medipim_api_key_value: '%app.medipim_api_key_value%'
   [10 lines ...]

This is my services :

#src/Services/MedipimApi.php
class MedipimApi
{

    const FILTER_CNK_AND_ACTIVE = 'filter_cnk_and_active';

    /**
     * @var MedipimApiV3Client
     */
    private MedipimApiV3Client $medipimApiV3Client;

    private TranslatorInterface $translator;

    /**
     * MedipimApi constructor.
     * @param string $medipim_api_key_id API key id for Medipim API call
     * @param string $medipim_api_key_value Api key (secret) value for Medipim API call
     * @param TranslatorInterface $translator TranslatorInterface object for I18N purpose
     */
    public function __construct(string $medipim_api_key_id, string $medipim_api_key_value, TranslatorInterface $translator)
    {
        $this->translator = $translator;
        $this->medipimApiV3Client = new MedipimApiV3Client($medipim_api_key_id, $medipim_api_key_value);
    }
....

When I try to call this service, I got this error :

Cannot autowire service "App\Services\MedipimApi": argument "$medipim_api_key_id" of method "__construct()" is type-hinted "string", you should configure its value explicitly.

It is working only when I copy the value of services.yaml and paste it into my services_env.yaml (where env is my current environment).

#config/services_dev.yaml
parameters:
    app.medipim_api_key_value: '%env(resolve:MEDIPIM_API_KEY)%'
    app.medipim_api_key_id: 326
services:
    [6 lines ...]
    # 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,Entity,Migrations,Tests,Kernel.php}'
    App\Services\MedipimApi:
        arguments:
            $medipim_api_key_id: '%app.medipim_api_key_id%'
            $medipim_api_key_value: '%app.medipim_api_key_value%'
   [10 lines ...]

Now it works!

Why did that work?

Symfony is suppose to load file as described here :

For the dev environment, Symfony loads the following config files and directories and in this order:

  • config/packages/*
  • config/packages/dev/*
  • config/services.yaml
  • config/services_dev.yaml

What did I miss?

EDIT

My kernel class looks right. It is loading services.yaml and then services_env.yaml as it should be :

class Kernel extends BaseKernel
{
    [1 line...]
    protected function configureContainer(ContainerConfigurator $container): void
    {
        $container->import('../config/{packages}/*.yaml');
        $container->import('../config/{packages}/'.$this->environment.'/*.yaml');

        if (is_file(\dirname(__DIR__).'/config/services.yaml')) {
            $container->import('../config/{services}.yaml');
            $container->import('../config/{services}_'.$this->environment.'.yaml');
        } elseif (is_file($path = \dirname(__DIR__).'/config/services.php')) {
            (require $path)($container->withPath($path), $this);
        }
    }
    [14 lines...]

Upvotes: 0

Views: 1195

Answers (1)

Lasouze
Lasouze

Reputation: 132

Why you configure param xx_id as int in your yml file ? In your service you type-hinted id as string !

Upvotes: -1

Related Questions