2016-08-09 45 views
2

請幫我瞭解我應該如何在我的服務中注入buzz http bundle以發送來自我的服務的請求。Symfony服務

我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: Uncaught PHP Exception Symfony\Component\Debug\Exception\FatalThrowableError: "Type error: Argument 4 passed to AppBundle\Utils\SendMessage::__construct() must be an instance of Buzz\Browser, none given, called in /app/var/cache/prod/appProdProjectContainer.php on line 270" at /app/src/AppBundle/Utils/SendMessage.php line 21 {"exception":"[object]

回答

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"

+0

非常感謝您!我想我需要睡覺,這是一個愚蠢的錯誤。 – Saba