Pros:
laravel/sanctum or spatie/laravel-api-docs) by auto-generating clients from specs.Cons:
Route::apiResource) may not map cleanly to specs.zircote/swagger-php).High for Laravel:
Http facade, GuzzleHttp\Client, or Symfony\Component\HttpClient via PSR-7/PSR-18.$app->bind('api.client', function ($app) {
$spec = $app['config']['api.spec'];
$generator = new \Jane\OpenApi\Generator();
return $generator->generate($spec, 'ApiClient');
});
throttle, auth) into generated clients via PSR-15 middleware.Http::fake(), Mockery for client mocking).Challenges:
config/api_specs/) or fetching them dynamically (e.g., from an API gateway).Cache::remember) if specs rarely change.array_shape) could enhance type hints but aren’t enforced by default. Use phpstan to validate generated code.Laravel Debugbar) to identify performance bottlenecks.config/api_specs/, remote API gateway)?composer post-update, Git hooks)?Http facade entirely, or coexist as a layer?HttpClientException) or use PSR-15?Http::fake(), JSON files)?ShouldBeValid or ShouldBeInvalid traits?Http facade with generated clients for type-safe, spec-driven requests.orders-service, payments-service) to replace manual Http::post() calls.Pilot Phase:
Http::macro() to wrap the generated client:
Http::macro('analytics', function ($app) {
$spec = $app['config']['api.analytics_spec'];
$generator = new \Jane\OpenApi\Generator();
return $generator->generate($spec, 'AnalyticsClient');
});
Tooling Setup:
composer.json script for regeneration:
"scripts": {
"post-autoload-dump": "php artisan api:generate",
"api:generate": "jane openapi:generate --spec=config/api_specs/{name}.yaml --output=app/ApiClients"
}
php artisan api:generate) to regenerate clients on demand.Incremental Adoption:
PaymentService, UserService).throttle or auth to generated clients).$app->singleton('api.stripe', function ($app) {
$spec = file_get_contents(config('api.specs.stripe'));
return app(\Jane\OpenApi\Generator::class)->generate($spec, 'StripeClient');
});
Api) to abstract client usage:
facade_root('Api', 'App\Facades\ApiFacade');
ApiRequestSent, ApiResponseReceived) for observability.Cache::remember('api.stripe_client', now()->addHours(1), function () {
return $generator->generate($spec, 'StripeClient');
});
x- extensions (e.g., x-php-name) but may need custom templates for Laravel-specific annotations.git push to main or api-spec-update tags).How can I help you explore Laravel packages today?