darkaonline/l5-swagger
Laravel package that wraps swagger-php and Swagger UI to generate and serve OpenAPI/Swagger documentation for your app. Provides Laravel-friendly installation, configuration, and routes to publish and view interactive API docs.
composer require darkaonline/l5-swagger
php artisan vendor:publish --provider="OpenApi\Generator\Laravel\L5SwaggerServiceProvider" --tag=l5-swagger-config
app/Http/Controllers/Api/UserController.php):
use OpenApi\Annotations as OA;
/**
* @OA\Get(
* path="/api/users",
* tags={"Users"},
* summary="Get all users",
* @OA\Response(response="200", description="List of users")
* )
*/
public function index()
{
return User::all();
}
php artisan l5-swagger:generate
/api/documentation (default route).@OA\Info in a base controller or service provider to define API metadata:
/**
* @OA\Info(title="My API", version="1.0")
*/
php artisan l5-swagger:generate to auto-scan annotated routes./api/documentation to see an interactive UI with your API endpoints.@OA\Get, @OA\Post, etc., for route-level docs.
/**
* @OA\Post(
* path="/api/users",
* tags={"Users"},
* summary="Create a user",
* @OA\RequestBody(
* required=true,
* @OA\JsonContent(ref="#/components/schemas/User")
* ),
* @OA\Response(response="201", description="User created")
* )
*/
public function store(Request $request)
{
// ...
}
@OA\Schema to define request/response schemas:
/**
* @OA\Schema(
* schema="User",
* type="object",
* required={"name", "email"},
* @OA\Property(property="name", type="string"),
* @OA\Property(property="email", type="string", format="email")
* )
*/
class User {}
// config/l5-swagger.php
'processors' => [
\OpenApi\Generator\Processor\FilterProcessor::class,
\App\Swagger\CustomProcessor::class, // Your custom processor
],
app/Swagger/CustomProcessor.php):
namespace App\Swagger;
use OpenApi\Generator\Processor;
class CustomProcessor extends Processor
{
public function process($operation, $filename, $context)
{
$operation['security'] = [['api_key' => []]]; // Add security globally
return $operation;
}
}
config/l5-swagger.php:
'securitySchemes' => [
'bearerAuth' => [
'type' => 'http',
'scheme' => 'bearer',
'bearerFormat' => 'JWT',
],
],
/**
* @OA\Get(
* path="/api/protected",
* tags={"Protected"},
* security={{"bearerAuth": {}}},
* @OA\Response(response="200", description="Protected data")
* )
*/
L5_SWAGGER_UI_DARK_MODE=true
L5_SWAGGER_UI_DOC_EXPANSION=false
.env or config/l5-swagger.php.@OA\Tag:
/**
* @OA\Tag(name="v1", description="API v1")
*/
class V1UserController {}
l5-swagger:generate with --spec flag:
php artisan l5-swagger:generate --spec=v1
php artisan l5-swagger:generate --spec=v2
php artisan vendor:publish --provider="OpenApi\Generator\Laravel\L5SwaggerServiceProvider" --tag=l5-swagger-assets
resources/views/vendor/l5-swagger/ui.blade.php to add custom JS/CSS.Annotation Parsing Issues:
@OA\* annotations instead).composer dump-autoload if annotations are ignored.Circular References in Schemas:
$ref to reference existing schemas:
@OA\Property(property="address", ref="#/components/schemas/Address")
Route Caching Conflicts:
php artisan route:cache) may break dynamic Swagger generation.Swagger UI Not Updating:
php artisan view:clear) or browser cache.?t= query param to bust cache:
/api/documentation?t= + timestamp.Environment-Specific Configs:
.env override config/l5-swagger.php, but may not apply to all environments.config('l5-swagger.*.key') in custom processors to handle environment-specific logic.Inspect Generated Spec:
/api/documentation/json or /api/documentation/yaml.Enable Verbose Logging:
// config/l5-swagger.php
'debug' => true,
storage/logs/l5-swagger.log.Check Processor Order:
config/l5-swagger.php. Reorder to prioritize custom logic.Validate Annotations:
php artisan l5-swagger:generate --debug to see parsing errors.Custom Generators:
\OpenApi\Generator\Laravel\Generator to support non-annotated sources (e.g., database schemas).Webhook Triggers:
generated event to auto-deploy docs:
// app/Providers/EventServiceProvider.php
protected $listen = [
\OpenApi\Generator\Events\Generated::class => [
\App\Listeners\DeploySwaggerDocs::class,
],
];
Dynamic Route Filtering:
FilterProcessor to exclude routes:
'processors' => [
\OpenApi\Generator\Processor\FilterProcessor::class => [
'exclude' => ['/api/debug', '/api/webhooks'],
],
],
Dark Mode Customization:
/* resources/css/swagger-ui-custom.css */
.swagger-ui .opblock-summary {
color: #fff !important; /* Override dark mode text */
}
Cache Generated Specs:
generate_always: false in config to avoid regenerating on every request.php artisan l5-swagger:generate.Exclude Unused Routes:
FilterProcessor to exclude admin/debug routes from docs.Optimize Schemas:
$ref to avoid duplication:
@OA\Schema(ref="#/components/schemas/User")
Lazy-Load Swagger UI:
How can I help you explore Laravel packages today?