simklee/laravel-openapi

Laravel OpenAPI

dev-main 2025-02-01 13:40 UTC

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"
CategoryKeyDescription
DocumentationtitleThe title of your API documentation
DocumentationversionThe API version
Documentationdoc_pathThe path where to store the api.json
Documentationroute_filterThe used filter for the routes
Generatordefault_query_paramsThe defined query params for each method
Strategiesuse_paginator_for_indexUsing a paginator for index route
Strategiesuse_dto_in_requestUsing a DTO inside the specific request
Strategiesuse_abstract_model_api_controllerUsing a abstract controller
Strategiesuse_resourcesUsing 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