Installation:
Add the bundle via Composer (ensure dev-master is used for latest features):
composer require djgxp/guzzle-bundle dev-master@dev
Enable in bundles.php:
Djgxp\Bundle\GuzzleBundle\DjgxpGuzzleBundle::class => ['all' => true],
Tag a Guzzle Client:
Register your Guzzle client with the guzzle.client tag in services.yaml:
services:
App\Service\ApiClient:
class: GuzzleHttp\Client
tags: ['name: guzzle.client']
First Use Case:
Inject the tagged client into a service and make a request. Open Symfony Profiler (/_profiler) to view logged requests under the "Guzzle" tab.
Request Logging: All requests made with tagged Guzzle clients are automatically logged in the Symfony Profiler. No manual instrumentation is required.
Integration with Existing Services:
class ApiService {
public function __construct(private ClientInterface $guzzleClient) {}
}
Environment-Specific Profiling:
Since the bundle is enabled for all environments (['all' => true]), ensure you restrict it to dev in production for performance:
Djgxp\Bundle\GuzzleBundle\DjgxpGuzzleBundle::class => ['dev' => true],
Customizing Logged Data:
Override the default profiler data collector by extending the bundle’s GuzzleDataCollector:
services:
guzzle.data_collector:
class: App\Profiler\CustomGuzzleDataCollector
decorates: djgxp_guzzle.data_collector
arguments: ['@guzzle.data_collector.inner']
No Request Filtering: The bundle logs all requests made by tagged clients. For sensitive APIs (e.g., payment gateways), exclude them by:
Guzzle 6 Only:
The bundle is incompatible with Guzzle 7+. If upgrading, consider alternatives like nelmio/cors-bundle or custom middleware.
Profiler Overhead: Logging requests adds minor overhead. Disable in production:
// config/packages/dev/djgxp_guzzle.yaml
djgxp_guzzle:
enabled: false
Verify Tagging: If requests aren’t logged, confirm the client is tagged:
bin/console debug:container | grep guzzle.client
Check Profiler Tab:
Ensure the "Guzzle" tab appears in the profiler. If missing, the bundle may not be loaded (check bundles.php).
Custom Data Formatting: Extend the collector to modify logged data (e.g., hide payloads):
class CustomGuzzleDataCollector extends Djgxp\Bundle\GuzzleBundle\DataCollector\GuzzleDataCollector {
public function collect(Request $request, ClientInterface $client, Transfer $transfer) {
$data = parent::collect($request, $client, $transfer);
$data['request']['body'] = '[REDACTED]'; // Mask sensitive data
return $data;
}
}
Add Metadata: Attach custom metadata to requests via middleware:
$stack->push(Middleware::tap(function (Request $request) {
$request = $request->withAttribute('custom_metadata', 'value');
}));
Event Listeners:
Listen to guzzle.request events to pre-process requests:
services:
app.guzzle_listener:
class: App\EventListener\GuzzleListener
tags:
- { name: kernel.event_listener, event: guzzle.request, method: onGuzzleRequest }
How can I help you explore Laravel packages today?