simonw
6 months ago
1111
5
Laravel

Why is injected class different to my provider registered class?

Posted 6 months ago by simonw

Hi all,

I'm registering guzzle as a ServiceProvider in my project so I can set it up with some important defaults. if I use app()->make() then it returns an instance of MY custom guzzle setup. However when I inject it in the constructor of a class, it's not my instance, it's the default guzzle instance with none of my settings.

Is there a way to set up the providers so that what is injected into my other services is my instance?

public function __construct(\GuzzleHttp\Client $client) {
    // $client is just the default guzzle client
}
public function __construct() {
    // $client here is my custom setup from the service provider.
    $client = app()->make()->('\GuzzleHttp\Client');
}

and here is my service provider register method

$this->app->bind('\GuzzleHttp\Client', function () {
    $stack = HandlerStack::create();
    $stack->push(Middleware::retry($this->decider(), $this->delay()));

    return new Client(['handler' => $stack, 'verify' => false]);
});

Please sign in or create an account to participate in this conversation.