symfonysymfony4autowired

Symfony4.2 Autowire bundle services


I want to autowire bundle service interface in my own services, but I get the error:

Cannot resolve argument $slackOauthService of "App\Controller\Panel\Slack\SigninController::afterOauth()": Cannot autowire service "App\Service\Slack\SlackOauthService": argument "$httpClient" of method "__construct()" references interface "GuzzleHttp\ClientInterface" but no such service exists. Did you create a class that implements this interface?

My code is:

public function __construct( ClientInterface $httpClient, EntityManagerInterface $em ) {
        $this->httpClient = $httpClient;
        $this->em         = $em;
    }

If I inject only the entity manager or a service defined by me, everything works fine, but whatever bundle service I try to inject (guzzle, browserkit, jms serializer etc) it does not work.

I have the default symfony services.yaml file defined for web projects:

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.

# 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']

I am using Symfony4.2.

What is the solution to this problem?


Solution

  • I have played around with this a while ago. Did you import the GuzzleHttp package? If so maybe this will help you:

    use GuzzleHttp\Client;
    
    public function __construct()
    {
       $this->http_client = new Client();
    }