【问题标题】:Symfony 4: "Autowire: you should configure its value explicitly."Symfony 4:“Autowire:你应该明确地配置它的值。”
【发布时间】:2019-04-05 12:47:46
【问题描述】:

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

我如何实例化我的类:

 /**
 * @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();
}

感谢您的帮助

【问题讨论】:

    标签: php symfony4


    【解决方案1】:

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

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

    检查所有信息到Defining Services Dependencies Automatically (Autowiring)

    【讨论】:

    • 我就是这么做的,没有任何改变。我什至试图把它放在 _defaults.bind 部分
    • 嘿@afilina,你看到下面的Karringan回答了吗?
    • 是的。那没有帮助。最后,我发现我的定义服务的 yaml 文件没有被加载,因此字符串 args 没有启动。
    【解决方案2】:

    您也可以通过在services.yml 文件中绑定参数来解决此问题。

    这里解释得很好https://stackoverflow.com/a/49084402

        # config.yml
        parameters:
          foo: 'secret'
    
        services:
          _defaults:
             autowire: true
             bind:
               $fooKey: '%foo%'
    

    【讨论】:

      猜你喜欢
      • 2019-07-16
      • 2011-11-21
      • 2018-06-03
      • 2010-10-05
      • 1970-01-01
      • 1970-01-01
      • 2018-12-13
      • 1970-01-01
      • 2023-03-16
      相关资源
      最近更新 更多