cirlmcesc/laravel-swaggerdoc
Generate OpenAPI 3.0 (Swagger) docs from your Laravel tests/commands. Write tests and produce an interface document automatically, inspired by swagger-php, to keep API documentation in sync with your code.
Installation
composer require cirlmcesc/laravel-swaggerdoc
Publish the config file:
php artisan vendor:publish --provider="Cirlmcesc\LaravelSwaggerdoc\SwaggerdocServiceProvider" --tag="config"
Configuration
Edit config/swaggerdoc.php to define:
api_title (e.g., "My API")api_version (e.g., "1.0.0")base_path (e.g., "/api")security_definitions (if using auth like JWT/OAuth2)output_path (default: storage/app/swagger.json)First Use Case Run the test command to generate the OpenAPI spec:
php artisan swaggerdoc:generate
Verify the output at storage/app/swagger.json or your configured path.
Test-Driven API Documentation
phpunit) with annotations like @swagger to define endpoints, parameters, and responses./** @swagger
* @OA\Get(
* path="/users",
* tags={"Users"},
* summary="Get all users",
* @OA\Response(response="200", description="List of users")
* )
*/
public function index() { ... }
php artisan swaggerdoc:generate to auto-generate swagger.json.Integration with Laravel Routes
SwaggerdocRouteServiceProvider to auto-discover and document routes:
// In AppServiceProvider@boot()
if ($this->app->environment('local')) {
$this->app->make(\Cirlmcesc\LaravelSwaggerdoc\SwaggerdocRouteServiceProvider::class);
}
Customizing the Spec
php artisan vendor:publish --provider="Cirlmcesc\LaravelSwaggerdoc\SwaggerdocServiceProvider" --tag="views"
resources/views/vendor/swaggerdoc/spec.blade.php to add/remove fields.Scheduled Generation
// app/Console/Kernel.php
protected function schedule(Schedule $schedule) {
$schedule->command('swaggerdoc:generate')->daily();
}
config/swaggerdoc.php:
'security_definitions' => [
'bearerAuth' => [
'type' => 'http',
'scheme' => 'bearer',
'bearerFormat' => 'JWT',
],
],
swagger.json via a route and use Swagger UI for visualization:
Route::get('/docs', function () {
return response()->file(storage_path('app/swagger.json'));
});
Annotation Parsing
@OA\Get). Ensure:
@OA tags).php artisan swaggerdoc:generate --verbose to see parsing errors.Route Caching
php artisan route:cache) may interfere with dynamic route discovery. Regenerate the spec after caching:
php artisan route:cache && php artisan swaggerdoc:generate
Namespace Conflicts
darkaonline/l5-swagger), ensure the package’s namespace (Cirlmcesc\LaravelSwaggerdoc) doesn’t clash. Override the provider binding if needed:
$this->app->bind(\Cirlmcesc\LaravelSwaggerdoc\SwaggerdocGenerator::class, function () {
return new \Custom\SwaggerdocGenerator();
});
Deprecated OpenAPI 2.0
3.0.0 in your spec:
'api_version' => '3.0.0',
Empty Spec Output:
--verbose to the generate command.base_path in config matches your API routes (e.g., /api vs. /v1).Missing Endpoints:
index(), store(), etc., explicitly.Schema Validation Errors:
swagger.json using Swagger Editor.Custom Generators
Cirlmcesc\LaravelSwaggerdoc\Generators\SwaggerdocGenerator to add logic (e.g., auto-documenting Eloquent models):
namespace App\Generators;
use Cirlmcesc\LaravelSwaggerdoc\Generators\SwaggerdocGenerator as BaseGenerator;
class CustomSwaggerdocGenerator extends BaseGenerator {
public function generate() {
parent::generate();
$this->documentModels();
}
}
Dynamic Annotations
getAnnotations() helper or libraries like rubix/ml-extra to parse annotations dynamically:
$reflection = new \ReflectionMethod(UserController::class, 'index');
$annotations = \Rubix\ML\Extra\Annotation::get($reflection);
Post-Processing
swaggerdoc.generated event to modify the spec:
// In AppServiceProvider@boot()
event(new \Cirlmcesc\LaravelSwaggerdoc\Events\SwaggerdocGenerated($spec));
public function handle(SwaggerdocGenerated $event) {
$event->spec['info']['x-custom'] = 'value';
}
Multi-Environment Configs
swaggerdoc.dev.php vs. swaggerdoc.prod.php):
'output_path' => env('SWAGGER_OUTPUT', storage_path('app/swagger.json')),
How can I help you explore Laravel packages today?