Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Guzzle Bundle Laravel Package

djgxp/guzzle-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. 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],
    
  2. 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']
    
  3. 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.


Implementation Patterns

Core Workflow

  1. Request Logging: All requests made with tagged Guzzle clients are automatically logged in the Symfony Profiler. No manual instrumentation is required.

  2. Integration with Existing Services:

    • Use dependency injection to inject the tagged Guzzle client into services:
      class ApiService {
          public function __construct(private ClientInterface $guzzleClient) {}
      }
      
    • The bundle ensures the client is properly configured for profiling.
  3. 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],
    
  4. 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']
    

Gotchas and Tips

Pitfalls

  1. No Request Filtering: The bundle logs all requests made by tagged clients. For sensitive APIs (e.g., payment gateways), exclude them by:

    • Using a separate, untagged Guzzle client.
    • Overriding the collector to filter requests by URI or method.
  2. Guzzle 6 Only: The bundle is incompatible with Guzzle 7+. If upgrading, consider alternatives like nelmio/cors-bundle or custom middleware.

  3. Profiler Overhead: Logging requests adds minor overhead. Disable in production:

    // config/packages/dev/djgxp_guzzle.yaml
    djgxp_guzzle:
        enabled: false
    

Debugging Tips

  1. Verify Tagging: If requests aren’t logged, confirm the client is tagged:

    bin/console debug:container | grep guzzle.client
    
  2. Check Profiler Tab: Ensure the "Guzzle" tab appears in the profiler. If missing, the bundle may not be loaded (check bundles.php).

  3. 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;
        }
    }
    

Extension Points

  1. Add Metadata: Attach custom metadata to requests via middleware:

    $stack->push(Middleware::tap(function (Request $request) {
        $request = $request->withAttribute('custom_metadata', 'value');
    }));
    
  2. 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 }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui