darkaonline/l5-swagger
Laravel wrapper for swagger-php and Swagger UI. Generate and serve OpenAPI/Swagger docs from annotations, with configurable routes, assets, and security (e.g., Passport). Includes config publishing, scanning paths, and an interactive docs UI.
Installation:
composer require darkaonline/l5-swagger
Publish the configuration file:
php artisan vendor:publish --provider="DarkaOnLine\L5Swagger\L5SwaggerServiceProvider" --tag=l5-swagger-config
Basic Configuration:
Edit config/l5-swagger.php to define your API paths and security schemes. Example:
'paths' => [
'api' => 'routes/api.php',
],
'securitySchemes' => [
'bearerAuth' => [
'type' => 'http',
'scheme' => 'bearer',
'bearerFormat' => 'JWT',
],
],
First Use Case: Add Swagger annotations to a controller method:
use DarkaOnLine\L5Swagger\Annotations as Swg;
/**
* @Swg\Get(
* path="/users",
* summary="Get a list of users",
* @Swg\Response(response=200, description="List of users")
* )
*/
public function index()
{
return User::all();
}
Access Swagger UI:
Visit /api/documentation (or your configured path) to see the interactive API documentation.
/**
* @Swg\Post(
* path="/users",
* summary="Create a new 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)
{
// ...
}
app/Models/ using @Swg\Schema:
/**
* @Swg\Schema(
* schema="User",
* @Swg\Property(property="name", type="string"),
* @Swg\Property(property="email", type="string", format="email")
* )
*/
class User extends Model {}
scan in l5-swagger.php to auto-discover routes:
'scan' => [
'app/Http/Controllers',
'app/Http/Api/Controllers',
],
'generator_factory' => function () {
return \OpenApi\Generator::create()
->withNamingConvention('underscore')
->withDefaultResponse();
},
l5-swagger.php:
'securitySchemes' => [
'oauth2' => [
'type' => 'oauth2',
'flows' => [
'password' => [
'tokenUrl' => 'oauth/token',
'scopes' => [
'read' => 'Read access',
'write' => 'Write access',
],
],
],
],
],
'securitySchemes' => [
'sanctum' => [
'type' => 'http',
'scheme' => 'bearer',
'bearerFormat' => 'token',
],
],
.env:
L5_SWAGGER_UI_DOC_EXPANSION=none
L5_SWAGGER_UI_FILTERS=true
L5_SWAGGER_UI_DARK_MODE=true
resources/views/vendor/l5-swagger/ui.blade.php.l5-swagger.php:
'apis' => [
'v1' => [
'paths' => ['routes/api.php'],
'title' => 'API v1',
'version' => '1.0.0',
],
'v2' => [
'paths' => ['routes/api-v2.php'],
'title' => 'API v2',
'version' => '2.0.0',
],
],
/api/v1/documentation and /api/v2/documentation.Route::middleware(['api', 'swagger.exclude'])->group(function () {
// Routes not documented in Swagger
});
AppServiceProvider:
public function boot()
{
if ($this->app->environment('local')) {
$this->loadL5Swagger();
}
}
$generator = Mockery::mock(\OpenApi\Generator::class);
$this->app->instance(\OpenApi\Generator::class, $generator);
$response = $this->get('/api/documentation');
$response->assertSee('Swagger UI');
l5-swagger.php:
'cache' => [
'enabled' => true,
'time' => 60, // Cache for 60 minutes
],
'scan' => [
'app/Http/Controllers/Api',
// Exclude non-API controllers
],
Annotation Parsing Issues:
doctrine/annotations is installed (v10+ includes it by default). For PHP 8.2+, use attributes:
#[Swg\Get(path: "/users", summary: "Get users")]
public function index() {}
storage/logs/l5-swagger.log for parsing errors.Route Conflicts:
/api/documentation).l5-swagger.php:
'routes' => [
'api' => [
'prefix' => 'docs',
'middleware' => ['web'],
],
],
Now accessible at /docs/documentation.Security Scheme Mismatches:
securitySchemes matches your auth setup (e.g., Passport/Sanctum). Example for Sanctum:
'security' => [
[
'sanctum' => [],
],
],
Model Documentation Not Reflecting:
@Swg\Schema annotations on models are ignored.scan:
'scan' => [
'app/Models',
'app/Http/Controllers',
],
Swagger UI Assets Not Loading:
L5_SWAGGER_UI_ASSETS_PATH in .env or clear cached views:
php artisan view:clear
Log Generation:
Enable debug logs in l5-swagger.php:
'debug' => true,
Logs are stored in storage/logs/l5-swagger.log.
Validate OpenAPI Spec:
Use the Swagger Validator to validate the generated spec at /api/documentation/json.
Check Generated Spec:
Access the raw OpenAPI spec at /api/documentation/json to inspect the output.
Processor Debugging: For custom processors, enable verbose output:
'processors' => [
'MyProcessor' => [
'class' => \App\Swagger\MyProcessor::class,
'config' => [
'verbose' => true,
],
],
],
How can I help you explore Laravel packages today?