csarrazi / CsaGuzzleBundle

A bundle integrating Guzzle >=4.0 in Symfony
250 stars 76 forks source link

Use with Symfony 4.4 #281

Closed rollsappletree closed 3 years ago

rollsappletree commented 3 years ago

Symfony 4.4 has deprecated Symfony\Bundle\FrameworkBundle\Controller\Controller, suggesting to go with Symfony\Bundle\FrameworkBundle\Controller\AbstractController.

Consider this example:

<?php

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class MyController extends AbstractController
{
    public function indexAction(String clientName)
    {

        $client = $this->get('csa_guzzle.'.$clientName);
        // ...
    }
}

will give you "Service "csa_guzzle.client.XXXX" not found: even though it exists in the app's container, the container inside "App\Controller\RedirectController" is a smaller service locator that only knows about the "http_kernel", "parameter_bag", "request_stack", "router" and "session" services. Try using dependency injection instead."

Any way to fix this?

csarrazi commented 3 years ago

As mentioned: use dependency injection instead of using the service locator ;)

E.g. https://symfony.com/doc/current/service_container/autowiring.html#an-autowiring-example

You should be able to inject a Client instance by adding the Client interface as the first argument of your indexAction.

Best regards, Charles

rollsappletree commented 3 years ago

Hi, I've gone with injecting the Container, 'cause I need to get the client based on runtime variable (such as client).

That is not the best solution (would have been with a service locator as you said) but I think is not possible to do as is.

Cheers C

clem983 commented 3 years ago

Hi, @rollsappletree I have the same problem but don't understand the solution. Could you give me an example please ? Thank you very much :)