Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

patrick.bennett@digitaladditive.com's avatar

Attempting to bind Guzzle Curl Client to Laravel's Service Container — then Type Hint the Client Fails when attempting to __construct()

So I figured I'd try to actually use this fancy IoC container in Laravel. I'm starting with Guzzle but I cannot get it to work. Perhaps there is a gap in my understanding. I really appreciate any help here.

so I've got a class for connecting to a RESTful Api. Here is a sample from it:

    use GuzzleHttp\Client;
    use GuzzleHttp\Subscriber\Oauth\Oauth1;

class EtApi {
    //you can pass in the model if you wanna
    //protected $model;

    //client Id
    protected $clientId;

    //client secret
    protected $clientSecret;

    //base_uri
    protected $getTokenUri;

    protected $client;

    //build
    function __construct(Client $client)
    {
        $this->client = $client;
        $this->clientId = 's0m3R4nd0mStr1nG';
        $this->clientSecret = 's0m3R4nd0mStr1nG';
        $this->getTokenUri = 'https://rest.api/requestToken';
        $this->accessToken = $this->getToken($this->clientId, $this->clientSecret, $this->getTokenUri);
    }
}

I've successfully installed and used Guzzle by manually newing it up inside of methods like $client = new Client(); but that's not very DRY and it's not the right way of doing things. So I created a ServiceProvider at app\Providers\GuzzleProvider.php. I made sure this was registered in app/config/app.php under $providers = ['App\Providers\GuzzleProvider']. Here is the Provider Code:


use Illuminate\Support\ServiceProvider;
use GuzzleHttp\Client;
use GuzzleHttp\Subscriber\Oauth\Oauth1;

class GuzzleProvider extends ServiceProvider {

    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
        $this->app->bind('Client', function () {
            return new Client;
        });
    }

}

So when I try to access my EtApi methods that load fails during the instantiation (__construct) with the following error.

ErrorException in EtApi.php line 23:
Argument 1 passed to App\EtApi::__construct() must be an instance of GuzzleHttp\Client, none given, called in /home/vagrant/webdocs/et_restful_test/app/Http/Controllers/EtConnectController.php on line 23 and defined

Do any of you Laravel Masters have any idea why I can't bind Guzzle using this code and Laravel's magic will just inject the obj into the constructor? The [docs1 say I should be able to do this. I must be missing something. Thank You!

0 likes
6 replies
ahuggins's avatar

Have you tried doing a composer dumpautoload? I mention it because you didn't...and always worth checking the basic stuff.

Also is the class EtApi the same as EtConnectController.php? It looks like it is not, so are you passing a Client object in the constructor of the EtConnectController?

usman's avatar

Try using full namespace in the register method of ServiceProvider:


    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
        $this->app->bind('GuzzleHttp\Client', function () {
            return new Client;
        });
    }

Usman.

bertfm's avatar

class EtApi extends Controller should work

Please or to participate in this conversation.