Symfony服务

3
请帮我找出应该如何将buzz http bundle注入到我的服务中,以从我的服务发送请求。
我的services.yml文件:
parameters:
    app_bundle.webUrl: https://url.com/
    app_bundle.Url: https://test.com
    app_bundle.token: rerwe9888rewrjjewrwj

services:
    app_bundle.send_message:
        class: AppBundle\Utils\SendMessage
        arguments: ["%app_bundle.webUrl%, %app_bundle.Url%, %app_bundle.token%, @buzz"]

My AppBundle\Utils\SendMessage

<?php

namespace AppBundle\Utils;

class SendMessage
{
    /**
     * SendMessage constructor.
     *
     * @param $webUrl
     * @param $Url
     * @param $token
     * @param Browser $buzz
     */
    public function __construct($webUrl, $Url, $token, Browser $buzz)
    {
        $this->webUrl = $webUrl;
        $this->Url = $Url;
        $this->token = $token;
        $this->buzz = $buzz;
    }

    /**
     * @param $action
     * @param null $data
     * @return mixed
     */
    private function sendRequest($action, $data = NULL)
    {
        $headers = array(
            'Content-Type' => 'application/json',
        );

        $response = $this->buzz->post($this->Url . $this->token . '/' . $action, $headers, json_encode($data));

        return $response;
    }
}

但是这导致了错误:

request.CRITICAL: 未捕获的PHP异常 Symfony\Component\Debug\Exception\FatalThrowableError: "类型错误: AppBundle\Utils\SendMessage::__construct()的第4个参数必须是Buzz\Browser的实例,但没有给出,调用位置在/app/var/cache/prod/appProdProjectContainer.php的第270行", 在/app/src/AppBundle/Utils/SendMessage.php的第21行 {"exception":"[object]

1个回答

3

服务配置文件中定义的每个参数都必须用双引号括起来,并用逗号分隔,就像上面的例子一样:

parameters:
    app_bundle.webUrl: https://url.com/
    app_bundle.Url: https://test.com
    app_bundle.token: rerwe9888rewrjjewrwj

services:
    app_bundle.send_message:
        class: AppBundle\Utils\SendMessage
        arguments: ["%app_bundle.webUrl%", "%app_bundle.Url%", "%app_bundle.token%", "@buzz"]

您在构造函数中只提供了一个字符串参数:"%app_bundle.webUrl%,%app_bundle.Url%,%app_bundle.token%,@buzz"

非常感谢!我想我需要睡觉了,犯了这么愚蠢的错误。 - Saba

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接