You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When writing code, I often inject my PSR dependencies like this:
public function log(\Psr\Log\LoggerInterface $log)
{
$log->info('some info');
var_dump($log::class); // Illuminate\Log\LogManager
}
public function cache(\Psr\SimpleCache\CacheInterface $cache)
{
$cache->set('someKey', 'someValue');
var_dump($cache::class); // Illuminate\Cache\Repository
}
This works great, and i love Laravel for making it so easy to use dependency injection :)
However what i really miss is a nice way to inject a PSR-18 HTTP Client, for example like this:
public function client(\Psr\Http\Client\ClientInterface $client)
{
$res = $client->get('https://example.com')->getReasonPhrase();
var_dump($res); // "OK"
var_dump($client::class); // GuzzleHttp\Client
}
Since guzzle is a composer suggestion, and most people will have it installed. I think it would be nice to have something like this in the framework:
if (class_exists(\GuzzleHttp\Client::class)) {
app()->bind(\Psr\Http\Client\ClientInterface::class, function ($app) {
return new \GuzzleHttp\Client();
});
app()->alias(\Psr\Http\Client\ClientInterface::class, 'http.client');
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
When writing code, I often inject my PSR dependencies like this:
This works great, and i love Laravel for making it so easy to use dependency injection :)
However what i really miss is a nice way to inject a PSR-18 HTTP Client, for example like this:
Since guzzle is a composer suggestion, and most people will have it installed. I think it would be nice to have something like this in the framework:
Beta Was this translation helpful? Give feedback.
All reactions