Getting started
Introduction
Generate the OpenAPI documents of a Laravel application from the code it already has.
Laravel OpenAPI Generator writes the OpenAPI documents of your Laravel application from what the application already declares: its routes and their middlewares, the signatures and the PHPDoc of the actions, the form requests and their validation rules, and the API resources, which it runs on sampled models. PHP attributes complete what the code cannot tell. Each document is written in the OpenAPI version you choose (3.0, 3.1 or 3.2) with OpenAPI Writer.
app/Http/Controllers/BookController.php
final class BookController
{
/**
* Add a book.
*
* @throws \App\Exceptions\DuplicateIsbnException
*/
public function store(StoreBookRequest $request): BookResource
{
return new BookResource(Book::query()->create($request->validated()));
}
}
| What the generator reads | What it writes |
|---|---|
Route::post('api/books', …)->middleware('auth:sanctum') | the path, the operation id, the Sanctum security scheme and a 401 |
| The PHPDoc | the summary (first line) and the description (the rest) |
StoreBookRequest::rules() | the request body, with every constraint, and a 422 |
The return type BookResource | a 201 with the Book component, built by running the resource on a sampled Book |
@throws DuplicateIsbnException | the error response the exception declares |
Compatibility
| Version | PHP | Laravel | Status |
|---|---|---|---|
| 1.x | ^8.3 | ^13.0 | Active |
| 0.6 | ^8.1 | ^10.0 || ^11.0 || ^12.0 || ^13.0 | End of life |
Upgrading from 0.6? Follow the upgrade guide.
Next
Install the package, then follow the quick start.