Installation
composer require lonban/l-swagger
Publish the config and views:
php artisan vendor:publish --provider="Lonban\Lswagger\LswaggerServiceProvider"
Basic Configuration
Edit config/lswagger.php to define:
api_prefix (e.g., api/v1)scan_paths (e.g., ['app/Http/Controllers'])auth (if API requires authentication)title and description for the docs.First Use Case: Annotate a Controller Add Swagger annotations to a controller method:
use Lonban\Lswagger\Annotation\Api;
/**
* @Api(
* path="/users",
* method="GET",
* summary="Get all users",
* tags={"Users"}
* )
*/
public function index()
{
return User::all();
}
Generate and View Docs Visit:
http://yourdomain/lswagger/api (API + docs)http://yourdomain/lswagger/docs (Docs only)Incremental Documentation
@Api for routes and @Param/@Body for request validation.Integration with Laravel Routing
@Api(path="route.name"):
Route::get('/users/{id}', [UserController::class, 'show'])->name('users.show');
/**
* @Api(path="route.users.show", method="GET")
*/
public function show($id) { ... }
Request/Response Modeling
/**
* @Api(
* path="/users",
* method="POST",
* summary="Create a user",
* @Body(model=UserRequest::class)
* )
*/
public function store(UserRequest $request) { ... }
/**
* @model
*/
class UserRequest {
/**
* @var string
* @required
*/
public $name;
}
Authentication Handling
auth in lswagger.php:
'auth' => [
'type' => 'http', // or 'apiKey', 'oauth2'
'name' => 'Authorization',
'in' => 'header',
'bearerFormat' => 'Bearer',
],
@Security to methods requiring auth:
/**
* @Api(path="/admin", method="GET")
* @Security(scheme="http")
*/
Grouping Endpoints
@Tag to categorize endpoints:
/**
* @Api(path="/users", method="GET", tags={"Users"})
* @Api(path="/users/{id}", method="GET", tags={"Users"})
*/
Testing Locally
php artisan lswagger:generate to regenerate docs on demand./lswagger/docs endpoint in your browser or with tools like Postman.CI/CD Pipeline
phpstan or custom scripts).- name: Generate Swagger Docs
run: php artisan lswagger:generate
- name: Upload Artifact
uses: actions/upload-artifact@v2
with:
name: swagger-docs
path: public/lswagger/docs
Versioning
scan_paths for different API versions.// config/lswagger-v2.php
'scan_paths' => ['app/Http/Controllers/V2'],
'api_prefix' => 'api/v2',
Custom Templates
php artisan vendor:publish --tag=lswagger-views
resources/views/lswagger/index.blade.php for branding or layout changes.Dynamic Paths
@Api(path):
/**
* @Api(path="/users/{id}", method="GET")
*/
public function show($id) { ... }
@Param:
/**
* @Param(name="id", type="integer", required=true, description="User ID")
*/
Error Handling
@Response:
/**
* @Api(path="/users", method="POST")
* @Response(status=422, model=ValidationError::class)
*/
Annotation Parsing Issues
/** */ (not /* */) for multi-line annotations.@Api(path) (URL-encode if needed).Caching Quirks
php artisan cache:clear
php artisan view:clear
php artisan lswagger:generate
Route Conflicts
/lswagger conflicts with existing routes.lswagger route in routes/web.php:
Route::prefix('lswagger')->middleware(['web'])->group(function () {
\Lonban\Lswagger\Lswagger::routes();
});
Model Validation Mismatches
@Body(model=...) doesn’t match request validation.@Property to override defaults:
/**
* @Property(type="string", format="date-time", example="2023-01-01T00:00:00Z")
*/
public $created_at;
Authentication Gaps
/lswagger/docs endpoint with tools like Swagger UI’s "Authorize" button.lswagger.php auth settings match your API’s actual auth flow.Performance with Large Codebases
scan_paths.scan_paths to only annotated controllers.['app/Http/Controllers', '!app/Http/Controllers/Test']).Enable Verbose Logging
Add to config/lswagger.php:
'debug' => true,
Check Laravel logs (storage/logs/laravel.log) for parsing errors.
Validate Annotations
Use a linter like PHPStan with the phpstan-phpdoc-parser extension to catch syntax issues.
Inspect Generated JSON
Visit /lswagger/api/json to see the raw Swagger JSON. Validate it with:
Swagger Editor.
Custom Annotations
Lonban\Lswagger\Annotation namespace).namespace App\Annotations;
use Lonban\Lswagger\Annotation\Annotation;
#[Attribute]
class DeprecatedSince extends Annotation {
public function __construct(public string $version) {}
}
config/lswagger.php:
'annotation_namespaces' => [
'Lonban\Lswagger\Annotation',
'App\Annotations',
],
Post-Processing Hooks
public function boot()
{
Lswagger::afterGenerate(function ($spec) {
$spec['info']['x-custom'] = 'value';
return $spec;
});
}
Dynamic Documentation
$spec = Lswagger::getSpec();
// Process $spec (e.g., export to
How can I help you explore Laravel packages today?