Symfony 4: "Autowire: you should configure its value explicitly."

user10562377 picture user10562377 · Nov 1, 2018 · Viewed 14.3k times · Source

I'm starting to working with Symfony4 and I meet the following error when I try to run my server: Cannot autowire service "App\Controller\CharacterInformation": argument "$region" of method "__construct()" is type-hinted "string", you should configure its value explicitly.

How I instantiate my class:

 /**
 * @Route("/")
 * @return Response
 */
function mainPage() {
    $characterInformation = new CharacterInformation('eu');
    return new Response($characterInformation->getCharacter());
}

The constructor of CharacterInformation:

 /**
 * @var int
 */
public function __construct(string $region) {
        $this->apiInformation = new ApiContent($region);
}

The constructor of ApiContent:

    public function __construct(string $region) {
    $apiToken = new ApiToken($region);
    $this->token = $apiToken->getToken();
    $this->region = $apiToken->getRegion();
}

Thanks for you help

Answer

Dmytro picture Dmytro · Nov 1, 2018

Try to set autowire information into config/services.yaml. Like:

#app.myservice.config:
    App\Controller\CharacterInformation:
        arguments:
            $region: "%region%"

Check all information into Defining Services Dependencies Automatically (Autowiring)