Symfony 4:“自动装配:您应该显式配置其值。”

5 php symfony4

我开始使用Symfony4,在尝试运行服务器时遇到以下错误:无法自动装配服务“ App \ Controller \ CharacterInformation”:方法“ __construct()”的参数“ $ region”被类型提示“字符串”,则应显式配置其值。

我如何实例化课程:

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

CharacterInformation的构造函数:

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

ApiContent的构造函数:

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

谢谢你的帮助

小智 8

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

#app.myservice.config:
    App\Controller\CharacterInformation:
        arguments:
            $region: "%region%"
Run Code Online (Sandbox Code Playgroud)

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

  • 是的。那没有帮助。最后,我发现定义服务的 yaml 文件没有被加载,因此字符串 args 没有启动。 (3认同)
  • 我就是这样做的,没有任何改变。我什至试图把它放在 _defaults.bind 部分 (2认同)