本文介绍了Symfony 4:“Autowire:你应该明确配置它的值."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用 Symfony4,但在尝试运行服务器时遇到以下错误:无法自动装配服务App\Controller\CharacterInformation":方法__construct() 的参数$region"" 是类型提示的字符串",您应该明确配置其值.

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.

我如何实例化我的类:

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

CharacterInformation 的构造函数:

The constructor of CharacterInformation:

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

ApiContent 的构造函数:

The constructor of ApiContent:

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

谢谢你的帮助

推荐答案

尝试将自动装配信息设置到 config/services.yaml.喜欢:

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

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

将所有信息检查到自动定义服务依赖项(Autowiring)

这篇关于Symfony 4:“Autowire:你应该明确配置它的值."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 04:16