Home > Net >  Symfony 6 - cannot resolve argument
Symfony 6 - cannot resolve argument

Time:09-27

I've created a class App\Service\Steam\SteamAPIRepository and it is listed when I run bin/console debug:container. My class constructor looks like this:

public function __construct(HttpClientInterface $httpClient, CacheInterface $cache, string $apiKey)
{
    $this->setCache($cache);
    $this->setHttpClient($httpClient);
    $this->setApiKey($apiKey);
    $this->setSteamUrl('http://api.steampowered.com/');
}

Before I added the $apiKey argument (and just had it hardcoded in the constructor), this was working fine and could be autowired in a controller method for example. I added the API key argument and inside config/services.yaml I did the following:

parameters:
  steam.apiKey: 'myapikey'

services:
  # after _defaults and App
  App\Service\Steam\SteamAPIRepository\:
    resource: '../src/Service/Steam/SteamAPIRepository.php'
    bind:
      $apiKey: '%steam.apiKey%'

But now I see the error:

Cannot resolve argument $steamAPIRepository of "App\Controller\App\IndexController::index()": 
Cannot autowire service "App\Service\Steam\SteamAPIRepository": 
argument "$apiKey" of method "__construct()" is type-hinted "string", 
you should configure its value explicitly.

Any ideas where I'm going wrong?

CodePudding user response:

I figured this out, I was making quite a simple mistake inside config/services.yaml. I replaced:

App\Service\Steam\SteamAPIRepository\:
  resource: '../src/Service/Steam/SteamAPIRepository.php'
  bind:
    $apiKey: '%steam.apiKey%'

with:

App\Service\Steam\SteamAPIRepository:
  arguments:
    $apiKey: '%steam.apiKey%'

Now this works as intended

  • Related