sefhi
sefhi

Reputation: 80

Problem when loading Service by injection

I'm trying to create a service, which can be injected anywhere. For this I am trying to pass as argument the HttpClient component of Symfony 4.3

I show you the service

https://i.sstatic.net/2384M.png

    <?php

namespace App\Service\Callback;


use Symfony\Component\HttpClient\HttpClient;

class Back
{

    private $client;

    public function __construct(HttpClient $httpClient)
    {
        $this->client = $httpClient::create();
    }

    public function sendCallback ( $method, $urlCallback, $option)
    {
        $response = $this->client->request($method,$urlCallback,$option);

        $statusCode = $response->getStatusCode();
        return $statusCode;
    }

}

Well, I'm trying to load it in the services.yml

# This file is the entry point to configure your own services.
# Files in the packages/ subdirectory configure your dependencies.

# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
    locale: 'en'
    base_url_front: '%env(BASE_URL_FRONT)%'
    mobile_type: 2

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.


    Nexy\Slack\Client: '@nexy_slack.client'

    Symfony\Component\HttpClient\HttpClient: '@http.client'

    # makes classes in src/ available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    App\:
        resource: '../src/*'
        exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'

    # controllers are imported separately to make sure services can be injected
    # as action arguments even if you don't extend any base controller class
    App\Controller\:
        resource: '../src/Controller'
        tags: ['controller.service_arguments']

    App\Service\Models\:
        resource: '../src/Service/Models'
        tags: ['@doctrine.orm.entity_manager','@nexy_slack.client']

    slack_client:
        class: 'App\Service\SlackClient'
        autowire : true
        arguments: ['@nexy_slack.client','@kernel']
        public : true

    callback_client:
        class: 'App\Service\Callback\Back'
        autowire: true
        arguments: ['@http.client']
        public: true

    App\Service\Apiclient\AteneaService:
        arguments: ["%kernel.environment%"]

    App\Service\Apiclient\UpmService:
        arguments: ["%kernel.environment%"]

    App\Service\Apiclient\HermesService:
        arguments: ["%kernel.environment%"]

    App\Service\Socket\:
        resource: '../src/Service/Socket'
        tags: ['@kernel','@nexy_slack.client']

The problem is that if I run php bin / console debug: autowiring in the terminal, to know if I created it, it returns the following error :

You have requested a non-existent service "http.client".

In the end what I want to achieve is something like this:

public function getClient(Back $back)
{

    $back->sendCallback('GET','http://vro-back.localhost:8888/callback/test');

}

But I can not because I can not inject it.

At the end if you look at the services.yml, I'm trying to create an alias for an HttpClient component, so I can pass it as an argument to the constructor of the Back class

And the route that I'm trying to load, exists ...

Symfony \ Component \ HttpClient \ HttpClient;

This is the component with which I am trying to work

https://symfony.com/doc/current/components/http_client.html

I would appreciate any help.

Upvotes: 4

Views: 9743

Answers (1)

slmder_h
slmder_h

Reputation: 201

You need to type-hint the interface instead

public function __construct(HttpClientInterface $httpClient)
{
    $this->httpClient = $httpClient;
}

And remove service.yaml configuration https://symfony.com/doc/current/components/http_client.html

Upvotes: 6

Related Questions