guzzle/common
guzzle/common provides the shared utility layer used by Guzzle: collections, event dispatching, caching helpers, and other common abstractions that power Guzzle HTTP clients and related packages. Useful when maintaining legacy Guzzle 3-based code.
Installation Add the package via Composer (though note this is a legacy Guzzle 3 component—ensure compatibility with your Laravel version, ideally using Guzzle 6/7 instead):
composer require guzzle/common:^3.0
For Laravel 5.5+, prefer modern Guzzle HTTP client (guzzlehttp/guzzle).
First Use Case
Use Guzzle\Common\Collection for grouping related data (e.g., API responses):
use Guzzle\Common\Collection;
$data = new Collection([
'users' => ['Alice', 'Bob'],
'roles' => ['admin', 'editor']
]);
echo $data['users'][0]; // Output: "Alice"
Key Classes to Explore
Collection: Lightweight key-value storage.EventDispatcher: For event-driven workflows (e.g., logging, caching).Filter\*: Data filtering utilities (e.g., Filter\GreaterThan).Data Aggregation
Use Collection to merge API responses or form data:
$formData = new Collection(request()->all());
$merged = $formData->merge(['meta' => ['timestamp' => now()]]);
// Laravel integration:
return response()->json($merged->toArray());
Event-Driven Logic
Attach listeners to Laravel events via EventDispatcher:
$dispatcher = new \Guzzle\Common\EventDispatcher();
$dispatcher->addListener('user.created', function ($event) {
Log::info('User created: ' . $event['user']->name);
});
Filtering Data Apply filters to Eloquent collections:
use Guzzle\Common\Filter\GreaterThan;
$users = User::all();
$filtered = (new GreaterThan('age', 25))->filter($users->toArray());
Collection as a singleton for global use:
$this->app->singleton('collection', function () {
return new \Guzzle\Common\Collection();
});
Collection for consistency:
$response = Http::get('https://api.example.com/data');
$data = new Collection(json_decode($response, true));
Guzzle 3 Deprecation
guzzlehttp/guzzle) for new projects.Collection vs. Laravel Collections
Guzzle\Common\Collection is not Laravel’s Illuminate\Support\Collection. Avoid mixing them without conversion:
$laravelCollection = collect($guzzleCollection->toArray());
EventDispatcher Quirks
$dispatcher->addListener('eloquent.saved', function ($model) {
event(new CustomEvent($model));
});
$value = (string) $collection['key'];
if (!is_numeric($age)) {
throw new \InvalidArgumentException('Age must be numeric');
}
Custom Filters
Extend Guzzle\Common\Filter\AbstractFilter for domain-specific logic:
class ActiveUserFilter extends AbstractFilter {
public function filter($items) {
return array_filter($items, fn($user) => $user['active']);
}
}
Event Subscribers Combine with Laravel’s event system for cross-cutting concerns:
class GuzzleEventSubscriber implements ShouldRegister {
public function handle() {
$dispatcher = new \Guzzle\Common\EventDispatcher();
$dispatcher->addListener('user.created', function () {
// Sync with external service
});
}
}
How can I help you explore Laravel packages today?