Symfony 4: “Autowire: you should configure its value explicitly.”

人盡茶涼 提交于 2019-12-12 10:50:22

问题


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


回答1:


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)




回答2:


Thank you Dmytro for you answer, it helped me to find the solution of the problem. In the services.yaml I added these line:

App\Controller\CharacterInformation: '@app.controller'

And the problem has been solved.



来源:https://stackoverflow.com/questions/53098835/symfony-4-autowire-you-should-configure-its-value-explicitly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!