graham-campbell/guzzle-factory
Simple factory for creating Guzzle HTTP clients with sensible defaults. Configure options like base_uri and get a ready-to-use client in one call. Supports PHP 7.4–8.5 and integrates cleanly into modern PHP projects.
Pros:
config/services.php). Aligns with Laravel’s dependency management patterns.Cons:
HttpClient for those). Not a replacement for full-fledged HTTP clients in high-performance scenarios.Stack Fit:
GuzzleHttp\Client instantiations.Http facade). No version conflicts expected.app(GuzzleFactory::class)->make(['base_uri' => '...'])). Supports dependency injection for testability.composer require graham-campbell/guzzle-factory). No runtime dependencies beyond Guzzle.Migration Path:
new GuzzleHttp\Client() calls in controllers, jobs, or commands with GuzzleFactory::make(). Start with non-critical APIs (e.g., analytics, logging).
// Before
$client = new GuzzleHttp\Client(['base_uri' => 'https://api.example.com']);
// After
$client = GuzzleFactory::make(['base_uri' => 'https://api.example.com']);
config/services.php:
// config/services.php
'guzzle' => [
'default' => [
'base_uri' => env('API_BASE_URI'),
'timeout' => 30,
'retries' => 3,
],
];
Bind the factory in a service provider:
$this->app->singleton(GuzzleFactory::class, function ($app) {
return new GuzzleFactory($app['config']['services.guzzle.default']);
});
Compatibility:
headers, auth, timeout, handler). Override defaults as needed.GuzzleFactory::makeWithHandler() (v4.2+). Useful for adding Laravel-specific middleware (e.g., caching, queueing).Mockery or PHPUnit to stub the factory:
$this->app->instance(GuzzleFactory::class, Mockery::mock(GuzzleFactory::class));
Low Risk:
Moderate Risk:
Mitigation:
config or environment variables to toggle factory usage gradually.GuzzleHttp\Client instantiations as a backup during migration.Architecture:
HttpClient).Http facade for simple requests? If so, assess whether the factory adds enough value to justify migration.Security/Compliance:
Team/Process:
Long-Term:
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton(GuzzleFactory::class, function ($app) {
return new GuzzleFactory($app['config']['services.guzzle']);
});
}
public function __construct(private GuzzleFactory $factory) {}
config/services.php:
'guzzle' => [
'default' => [
'base_uri' => env('API_BASE_URI'),
'timeout' => 30,
'retries' => [
'max_attempts' => 3,
'retry_delay' => 100,
'except' => [400, 404],
],
'headers' => [
'Accept' => 'application/json',
'User-Agent' => 'MyApp/1.0',
],
],
];
// app/Facades/Guzzle.php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class Guzzle extends Facade { protected static function getFacadeAccessor() { return 'guzzle.factory'; } }
Bind it in a service provider:
$this->app->bind('guzzle.factory', function ($app) {
return $app->make(GuzzleFactory::class
How can I help you explore Laravel packages today?