首页 文章

Symfony服务

提问于
浏览
2

请帮助我了解如何在我的服务中注入buzz http包以从我的服务发送请求 .

我的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"]

我的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的实例,没有给定,调用第270行的/app/var/cache/prod/appProdProjectContainer.php在/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"

相关问题