Weave 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

Plugin Laravel Package

guzzle/plugin

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Add the package via Composer (note: this is for Guzzle 3, which is outdated; ensure compatibility with your Laravel version):

    composer require guzzle/plugin:^3.0
    

    Check Laravel's Guzzle HTTP client version (vendor/guzzlehttp/guzzle) to confirm compatibility.

  2. Basic Usage Register a plugin with Guzzle’s client:

    use Guzzle\Plugin\PluginInterface;
    use Guzzle\Plugin\BeforePluginInterface;
    use Guzzle\Http\Message\RequestInterface;
    
    $client = new \Guzzle\Http\Client();
    $client->addSubscriber(new class implements BeforePluginInterface {
        public function before(RequestInterface $request) {
            // Modify request (e.g., add headers)
            $request->getHeaders()->add('X-Custom-Header', 'value');
        }
    });
    
  3. First Use Case Use a plugin to log requests/responses for debugging:

    $client->addSubscriber(new \Guzzle\Plugin\Log\LogPlugin());
    

Implementation Patterns

Common Workflows

  1. Request/Response Modification Use BeforePluginInterface/AfterPluginInterface to intercept and alter requests/responses:

    $client->addSubscriber(new class implements AfterPluginInterface {
        public function after($event, $request) {
            if ($event['result'] instanceof \Guzzle\Http\Message\Response) {
                $response = $event['result'];
                // Parse JSON response
                $data = json_decode($response->getBody(), true);
                // Modify data before returning
            }
        }
    });
    
  2. Middleware-Like Behavior Chain plugins for layered functionality (e.g., auth + caching):

    $client->addSubscriber(new AuthPlugin());
    $client->addSubscriber(new CachePlugin());
    
  3. Laravel Integration Extend Laravel’s HTTP client (app/Http/Client.php):

    $client = app('http_client');
    $client->getClient()->addSubscriber(new CustomPlugin());
    

Integration Tips

  • Dependency Injection: Bind plugins to Laravel’s service container for reuse:
    $this->app->singleton('custom.plugin', function () {
        return new CustomPlugin();
    });
    
  • Configuration: Store plugin settings in config/services.php:
    'plugins' => [
        'auth' => [
            'token' => env('API_TOKEN'),
        ],
    ],
    
  • Testing: Mock plugins in PHPUnit:
    $mockPlugin = $this->getMockBuilder(CustomPlugin::class)
        ->disableOriginalConstructor()
        ->getMock();
    $client->getClient()->addSubscriber($mockPlugin);
    

Gotchas and Tips

Pitfalls

  1. Guzzle 3 Deprecation

    • This package is for Guzzle 3, which is unsupported (Laravel uses Guzzle 6/7).
    • Workaround: Use guzzlehttp/guzzle plugins (e.g., guzzlehttp/middleware) instead.
  2. Plugin Order Matters

    • Plugins execute in registration order. Use AfterPluginInterface carefully to avoid race conditions.
  3. Memory Leaks

    • Avoid storing large data in plugins (e.g., caching responses globally). Use Laravel’s cache instead.

Debugging

  • Log Events: Use Guzzle\Plugin\Log\LogPlugin to inspect request/response cycles:
    $client->addSubscriber(new \Guzzle\Plugin\Log\LogPlugin([
        'log' => \Monolog\Logger::getInstance('guzzle'),
    ]));
    
  • Disable Plugins Temporarily: Comment out plugin registrations to isolate issues.

Extension Points

  1. Custom Events Extend Guzzle\Plugin\Event for domain-specific logic:
    class CustomEvent extends \Guzzle\Plugin\Event {
        public function __construct($request, $response, $data) {
            parent::__construct($request, $response);
            $this->data = $data;
        }
    }
    
  2. Plugin Composition Combine plugins into a single subscriber for cleaner code:
    $client->addSubscriber(new class implements SubscriberInterface {
        public function handle($event) {
            (new AuthPlugin())->handle($event);
            (new CachePlugin())->handle($event);
        }
    });
    
  3. Laravel Events Trigger Laravel events from plugins:
    event(new RequestHandled($request, $response));
    
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.
tequilarapido/trackit
gregsphotography/site-builder-filament
cabaoctavio/laravel-query-log
gregsphotography/site-builder-components
io-digital/clickatell
snowsoft/laravel-admin
oxygen/auth
relicsoftware/barakah-payment-gateway
tobya/work-with-composer
equipc/laravel-email-validator
equipc/laravel-email-database-log
adedsec/nova-translated-labels
wazza/laravel-db-encryption
hokny/laravel-generator
vonsogt/backup
jengo/queues
jeremykenedy/laravel-observability
coleus/frame
thtg88/journalism
filagora/filagora-ai-forums