Saba Saba - 4 months ago 29
PHP Question

Symfony services

Please help me to find out how I should inject buzz http bundle in my service to send request from my service.

My 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;
}
}


but this caused the error:


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]

Answer

Every argument defined in the service configuration file must be wrapped in double quotes and separated by a comma, as the example above:

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

You provided only one string argument to the constructor: "%app_bundle.webUrl%, %app_bundle.Url%, %app_bundle.token%, @buzz"