首页 > 解决方案 > Symfony 4:“Autowire:你应该明确地配置它的值。”

问题描述

我开始使用 Symfony4,当我尝试运行我的服务器时遇到以下错误:无法自动连接服务“App\Controller\CharacterInformation”:方法“__construct()”的参数“$region”是类型提示的“ string”,你应该明确地配置它的值。

我如何实例化我的类:

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

CharacterInformation 的构造函数:

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

ApiContent 的构造函数:

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

谢谢你的帮助

标签: phpsymfony4

解决方案


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

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

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


推荐阅读