saloonphp/saloon
Saloon is a Laravel/PHP-friendly HTTP client framework for building typed API connectors and requests. It supports middleware, authentication, retries, caching, testing/mocking, and async features, helping you create clean, reusable integrations without boilerplate.
Saloon aligns well with Laravel’s dependency injection (DI), service container, and testing-first paradigms. Its connector/request/response pattern mirrors Laravel’s Eloquent/Query Builder abstractions, making it intuitive for PHP developers. The package’s plugin-based middleware system (e.g., retries, auth) integrates seamlessly with Laravel’s middleware pipeline, enabling consistent error handling and logging.
Key Fit Points:
SaloonServiceProvider, SaloonFacade).Spatie’s Data Transfer Objects (if used), reducing boilerplate for API payloads.MockClient) and assertions (assertSent, assertSentInOrder) align with Laravel’s HttpTests and Pest/PHPUnit workflows.Low to Medium Effort for Laravel projects already using:
Http::get() calls with structured, reusable connectors.Challenges:
Http clients to Saloon’s Connector/Request pattern.curl or file_get_contents will need significant refactoring.HttpClient) may require wrapper classes for full parity.| Risk Area | Severity | Mitigation |
|---|---|---|
| Breaking Changes (v4.0.0) | High | Test thoroughly; v4 fixes critical CVEs (SSRF, RCE) but introduces BC changes. |
| Performance Overhead | Medium | Benchmark against Guzzle; Saloon adds abstraction layers (e.g., middleware). |
| Testing Complexity | Low | Mocking utilities reduce test flakiness but require learning Saloon’s syntax. |
| Dependency Bloat | Low | Saloon is lightweight (~10MB with dependencies); no major PHP extensions. |
| Auth Complexity | Medium | Supports OAuth, API keys, and custom auth, but misconfigurations may cause issues. |
Http::* calls, or only new API integrations?curl) that can’t be migrated?MockClient vs. Laravel’s Http::fake()?)HttpException/Throwable hierarchy?singleton)?env() + Saloon’s defaultAuth?)Saloon is optimized for:
Compatibility Matrix:
| Laravel Feature | Saloon Compatibility | Notes |
|---|---|---|
| Service Container | ✅ Full support (bind connectors as singletons) | Use SaloonServiceProvider. |
HTTP Client (Http::*) |
⚠️ Partial (wrapper needed) | Saloon is a replacement, not a wrapper. |
| Queues/Jobs | ✅ Yes | Send requests in jobs with dispatch(). |
Testing (Http::fake()) |
✅ Enhanced (MockClient) | More granular than Laravel’s faker. |
| Packages (e.g., Spatie) | ✅ Plugins (e.g., saloonphp/xml-wrangler) |
Extendable via middleware. |
Phase 1: Pilot Integration (2-4 weeks)
Http:: calls.$this->app->singleton(StripeConnector::class, fn() => new StripeConnector());
$customer = Saloon::connector(StripeConnector::class)
->send(new GetCustomerRequest(['customer_id' => 'cus_123']));
Http::fake() with MockClient in unit tests:
$mock = new MockClient();
$mock->shouldReceive('send')
->once()
->andReturn(new Response($mock, 200, [], '{"id": "123"}'));
Phase 2: Full Adoption (4-8 weeks)
Http::get()/post() to Saloon Request classes.Macroable trait) to add custom methods:
GetCustomerRequest::macro('withSubscription', fn($subId) => [
'query' => ['subscription' => $subId]
]);
$connector->addMiddleware(new RetryMiddleware());
deprecate() helper to phase out old Http::* calls.Phase 3: Optimization (Ongoing)
CacheMiddleware or Laravel’s cache.saloonphp/rate-limiter).Sentry or Laravel Debugbar.| Priority | Task | Dependencies |
|---|---|---|
| High | Migrate critical APIs (e.g., payments) | Stakeholder buy-in, testing strategy. |
| Medium | Replace Http::* in services/controllers |
Pilot results, team training. |
| Low | Add Saloon to new features | Documentation, CI/CD updates. |
Rollback Plan:
Http:: and Saloon code during transition.Pros:
Request classes encapsulate API logic (e.g., endpoints, auth).Connector).OAuth2 for JWT).Cons:
Connector/Request pattern.debug() helper).Maintenance Tasks:
| Task | Frequency | Owner |
|---|---|---|
| Update Saloon/Laravel dependencies | Quarterly | DevOps/Backend Team |
| Review API contract changes | Bi-annually | Product/Backend Team |
| Audit mock tests | Monthly | QA/Backend Team |
Common Issues & Solutions:
| Issue | Solution |
|---|---|
| Authentication failures | Use Saloon’s NullAuthenticator for testing; validate defaultAuth. |
| SSRF/cred |
How can I help you explore Laravel packages today?