darkaonline/l5-swagger
Laravel-friendly wrapper around swagger-php and Swagger UI. Generate and serve OpenAPI/Swagger docs from annotations, with configurable routes, UI, and assets. Includes install/config guides, migration notes, tips, and Passport auth examples.
Installation:
composer require darkaonline/l5-swagger
php artisan vendor:publish --provider="DarkaOnLine\L5Swagger\L5SwaggerServiceProvider" --tag=l5-swagger-config
Publish the config file to config/l5-swagger.php.
Basic Configuration:
Update .env with your API base path:
L5_SWAGGER_FORMAT=json
First Use Case: Add OpenAPI attributes to a controller method:
use DarkaOnLine\L5Swagger\Annotations as SWG;
class UserController extends Controller
{
/**
* @SWG\Get(
* path="/users",
* summary="Get all users",
* @SWG\Response(response=200, description="List of users")
* )
*/
public function index()
{
return User::all();
}
}
Access Swagger UI at /api/documentation.
Annotation-Based Documentation: Use PHP attributes (or annotations) to define API endpoints, parameters, and responses:
/**
* @SWG\Post(
* path="/users",
* summary="Create a user",
* @SWG\Parameter(
* in="body",
* name="user",
* required=true,
* @SWG\Schema(ref="#/definitions/User")
* ),
* @SWG\Response(response=201, description="User created")
* )
*/
public function store(Request $request)
{
// ...
}
Dynamic API Definition: Generate OpenAPI specs programmatically in service providers or bootstrappers:
public function boot()
{
$openapi = \OpenApi\Generator::scan([app_path('Http/Controllers')]);
file_put_contents(public_path('openapi.json'), $openapi->toJson());
}
Swagger UI Customization:
Extend the default UI via config/l5-swagger.php:
'ui' => [
'display' => [
'deep_linking' => true,
'doc_expansion' => 'none',
],
'theme' => 'dark',
],
Authentication Integration:
Configure OAuth2/Passport in config/l5-swagger.php:
'securityDefinitions' => [
'bearerAuth' => [
'type' => 'apiKey',
'name' => 'Authorization',
'in' => 'header',
],
],
L5Swagger\Middleware\Documentation to exclude routes from Swagger generation:
Route::middleware(['api', 'documentation:ignore'])->group(function () {
// Routes to exclude
});
$this->get('/api/documentation')
->assertSee('Get all users');
swagger_ui view to merge multiple OpenAPI specs:
return view('swagger-ui', [
'specs' => [
['url' => '/api/v1/documentation', 'label' => 'API v1'],
['url' => '/api/v2/documentation', 'label' => 'API v2'],
],
]);
Annotation Parsing Issues:
swagger-php processors are correctly configured in config/l5-swagger.php:
'processors' => [
'DarkaOnLine\L5Swagger\Processors\Annotations',
'DarkaOnLine\L5Swagger\Processors\Attributes',
],
php artisan l5-swagger:generate to regenerate specs after changes.Caching Conflicts:
php artisan view:clear
php artisan cache:clear
Environment-Specific Configs:
L5_SWAGGER_FORMAT):
L5_SWAGGER_FORMAT=json # Override config value
PHP Attributes vs. Annotations:
#[SWG\Get]), while older versions rely on docblocks.doctrine/annotations for backward compatibility:
composer require doctrine/annotations
Log Generation: Enable debug logs in config/l5-swagger.php:
'debug' => true,
Logs appear in storage/logs/l5-swagger.log.
Validate Specs: Use the Swagger Validator to test generated specs:
php artisan l5-swagger:validate
Asset Paths: Ensure L5_SWAGGER_UI_ASSETS_PATH in .env points to a valid directory:
L5_SWAGGER_UI_ASSETS_PATH=public/swagger-ui
Custom Processors:
Extend DarkaOnLine\L5Swagger\Processors\BaseProcessor to add custom logic (e.g., for GraphQL or gRPC).
Swagger UI Themes: Override the default theme by publishing assets:
php artisan vendor:publish --tag=l5-swagger-assets
Then customize resources/views/vendor/l5-swagger/swagger-ui.blade.php.
API Versioning: Use route groups to isolate Swagger specs by version:
Route::prefix('api/v1')->group(function () {
// v1 routes with @SWG\Tag(name="v1")
});
Security Schemes: Dynamically add OAuth2/Passport schemes via service providers:
$this->app->singleton(\OpenApi\Annotations\OpenApi::class, function () {
$openapi = new \OpenApi\Annotations\OpenApi();
$openapi->securitySchemes = [
'bearerAuth' => [
'type' => 'http',
'scheme' => 'bearer',
'bearerFormat' => 'JWT',
],
];
return $openapi;
});
How can I help you explore Laravel packages today?