simklee / laravel-openapi
Laravel OpenAPI
Requires
- php: ^8.2|^8.3
- ext-json: *
- ext-mbstring: *
- nikic/php-parser: ^5.0
- simklee/laravel-workbench: *
This package is auto-updated.
Last update: 2025-02-01 12:40:21 UTC
README
The package Laravel OpenAPI generates a whole API for your models and creates a OpenAPI (Swagger) documentation by analysing the API routes and the generated code.
Installation
composer require simklee/laravel-openapi
After installation the command "openapi:install" will triggered through composer post install script.
It publishes the assets for the Swagger UI and creates "routes/api.php" (if not exists) and activates the API routes in "bootstrap/app.php" if not already activated.
Usage
Migrate your model before generating API! The generator fetches the column informations of a model from the database.
Configuration
Publish the configuration for ths package if you want to modify the settings.
php artisan vendor:publish --provider="SimKlee\LaravelOpenApi\LaravelOpenApiServiceProvider"
Category | Key | Description |
---|---|---|
Documentation | title | The title of your API documentation |
Documentation | version | The API version |
Documentation | doc_path | The path where to store the api.json |
Documentation | route_filter | The used filter for the routes |
Generator | default_query_params | The defined query params for each method |
Strategies | use_paginator_for_index | Using a paginator for index route |
Strategies | use_dto_in_request | Using a DTO inside the specific request |
Strategies | use_abstract_model_api_controller | Using a abstract controller |
Strategies | use_resources | Using a resource class for the JSON response |
Create API for a model
Depending on the used configuration the following files will be generated or modified:
- Abstract controller class (config: strategies.use_abstract_model_api_controller)
- Controller class
- Routes class
- Specific request classes for each API method
- DTOs for the request classes (config: strategies.use_dto_in_request)
- Resource classes for each API method (config: strategies.use_resources)
- Activating routes in "routes/api.php" by using specific routes class
php artisan openapi:generate:route
Remove API for a model
All generated files will be removed and the routes removed from "routes/api.php".
php artisan openapi:remove:route
Create OpenAPI (Swagger) documentation
The documentation will be generated based on the activated routes in "api.php" by analysing the generated code.
You can browse the Swagger documentation by calling the path "/swagger".
php artisan openapi:generate:doc