php-http/plugins
Deprecated plugin collection for HTTPlug (php-http/plugins). Since v1.1 most plugins moved to php-http/client-common; logger, cache, and stopwatch live in separate packages. Use this package only for legacy compatibility.
Installation
composer require php-http/plugins
(Note: This package is deprecated as of v1.1.0. Most functionality has been merged into php-http/client-common and standalone plugins like php-http/cache-plugin. Use these instead for new projects.)
Basic Usage (Legacy)
use Http\Client\Common\PluginClient;
use Http\Client\Common\Plugin\PluginInterface;
$client = new \GuzzleHttp\Client();
$pluginClient = new PluginClient($client);
// Add a plugin (e.g., logging)
$pluginClient->addPlugin(new class implements PluginInterface {
public function handleRequest($request, callable $next) {
// Modify request
return $next($request->withHeader('X-Legacy', 'Plugin'));
}
});
$response = $pluginClient->sendRequest($request);
First Use Case (Migrate to Modern Alternatives)
php-http/logger-plugin.
composer require php-http/logger-plugin
use Http\Client\Common\Plugin\LoggerPlugin;
use Http\Client\Common\PluginClient;
$client = new PluginClient(new \GuzzleHttp\Client());
$client->addPlugin(new LoggerPlugin());
php-http/cache-plugin.
composer require php-http/cache-plugin
use Http\Client\Common\Plugin\CachePlugin;
use Http\Client\Common\PluginClient;
$client = new PluginClient(new \GuzzleHttp\Client());
$client->addPlugin(new CachePlugin());
Stackable Plugins: Plugins execute in reverse order of addition (LIFO).
$client->addPlugin(new AuthPlugin());
$client->addPlugin(new LoggingPlugin());
(Requests pass through LoggingPlugin first, then AuthPlugin.)
Conditional Logic: Use closures for dynamic behavior.
$client->addPlugin(new class implements PluginInterface {
public function handleRequest($request, callable $next) {
if ($request->getUri()->getPath() === '/admin') {
return $next($request->withHeader('X-Admin', 'true'));
}
return $next($request);
}
});
PSR-18 Clients with Plugins
Use httplug/httplug for PSR-18 compliance:
use Http\Client\Common\PluginClient;
use Http\Client\Common\Plugin\PluginInterface;
use Http\Client\Common\Plugin\CachePlugin;
use Http\Client\Common\Plugin\LoggerPlugin;
use Http\Client\Common\Plugin\StopwatchPlugin;
$client = new PluginClient(new \GuzzleHttp\Client());
$client->addPlugin(new CachePlugin());
$client->addPlugin(new LoggerPlugin());
$client->addPlugin(new StopwatchPlugin());
Laravel Integration (v8+) Bind plugins via service provider:
use Http\Client\Common\PluginClient;
use Illuminate\Support\ServiceProvider;
class HttpPluginServiceProvider extends ServiceProvider {
public function register() {
$this->app->singleton('http.client', function ($app) {
$client = new PluginClient(new \GuzzleHttp\Client());
$client->addPlugin(new \Http\Client\Common\Plugin\LoggerPlugin());
return $client;
});
}
}
Facade Usage (Laravel)
use Illuminate\Support\Facades\Http;
// Modern Laravel HTTP client (v8+) with middleware
Http::withOptions(['debug' => true])->get('https://api.example.com');
php-http/stopwatch-plugin for timing).LoggerPlugin.php-http/retry-plugin.php-http/plugins: This package is fully deprecated. Migrate to:
php-http/client-common (core plugins).logger, cache, stopwatch).guzzlehttp/guzzle v7+ with PSR-7).Http client with middleware over legacy plugins.dd($request) in each plugin.MessageInterface instances (avoid direct mutations).
// ❌ Avoid
$request->withHeader('X-Foo', 'bar');
// ✅ Correct
$request = $request->withHeader('X-Foo', 'bar');
CachePlugin) for static endpoints.PluginInterface for reusable logic:
use Http\Client\Common\Plugin\PluginInterface;
class RateLimitPlugin implements PluginInterface {
public function handleRequest($request, callable $next) {
// Rate-limiting logic
return $next($request);
}
}
$next($request) in try-catch for graceful failures.Http::withOptions(['timeout' => 5])->post('/api', $data);
app/Http/Kernel.php:
protected $middleware = [
\App\Http\Middleware\TransformResponse::class,
];
$this->app->singleton(\Http\Client\Common\Plugin\LoggerPlugin::class);
How can I help you explore Laravel packages today?