g4t / swagger
g4t laravel swagger
- dev-main
- 4.1.10
- 4.1.9
- 4.1.8
- 4.1.7
- 4.1.6
- 4.1.5
- 4.1.4
- 4.1.3.x-dev
- 4.1.1.x-dev
- 4.1.1
- 4.1.0.x-dev
- 4.1.0
- 4.0.2
- 4.0.1.x-dev
- 4.0.1
- 4.0.0.x-dev
- 4.0.0
- 3.2.3.x-dev
- 3.2.3
- 3.2.2.x-dev
- 3.2.2
- 3.2.1.x-dev
- 3.2.1
- 3.2.0
- 3.1.19.x-dev
- 3.1.19
- 3.1.18.x-dev
- 3.1.18
- 3.1.17
- 3.1.16
- 3.1.15
- 3.1.14
- 3.1.13
- 3.1.12
- 3.1.11
- 3.1.10
- 3.1.9
- 3.1.8
- 3.1.7
- 3.1.6
- 3.1.5
- 3.1.4
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1.0
- 3.0.9
- 3.0.8
- 3.0.7.x-dev
- 3.0.7
- 3.0.6.x-dev
- 3.0.6
- 3.0.5.x-dev
- 3.0.5
- 3.0.4.x-dev
- 3.0.4
- 3.0.3.x-dev
- 3.0.3
- 3.0.2.x-dev
- 3.0.2
- 3.0.1.x-dev
- 3.0.1
- 3.0.0.x-dev
- 3.0.0
- 2.0.7
- 2.0.6.x-dev
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3.x-dev
- 2.0.3
- 2.0.2.x-dev
- 2.0.2
- 2.0.1.x-dev
- 2.0.1
- 2.0.0.x-dev
- 2.0.0
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3.x-dev
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- dev-development
- dev-dev
This package is auto-updated.
Last update: 2026-04-05 18:58:55 UTC
README
The Swagger Laravel Autogenerate Package automatically generates OpenAPI (Swagger) documentation for your Laravel APIs from route definitions, validation rules, and optional controller inference—so you spend less time maintaining specs by hand.
Documentation: https://swagger.g4t.io/
Features
- Mock workflow (experimental): optional
swagger:mock-server. - Cached spec:
swagger:cacheandmake:swagger. - Generates OpenAPI 3.x JSON from your registered routes (URI, methods, middleware, tags, etc.).
- FormRequest rules drive request schemas and query/path parameters where applicable.
- Optional inferred response examples from controllers (
infer_response_examples): literals,JsonResource::collection/::make/new Resource,response()->json(...), and more (seeResponseExampleExtractor). - Optional inferred error examples (
infer_error_response_examples):response()->json([...], status),abort(), validation-style 422 when rules warrant it. - Faster JSON endpoint: when
load_from_jsonisfalse, the package can servepublic/{cached_spec_path}if it exists (use_cached_spec_when_present, defaulttrue), otherwise builds from routes on each request. - storage/swagger/… JSON files can override per-route response examples when enabled in config.
- Route macros:
->description(),->summary(),->hiddenDoc(); controller#[SwaggerSection('…')]. - Optional basic auth for the documentation UI (configurable).
Installation
composer require g4t/swagger
Usage
Video walkthrough
-
Publish the configuration file:
php artisan vendor:publish --provider "G4T\Swagger\SwaggerServiceProvider" -
Adjust
config/swagger.php(title, URL prefix, versions, inference flags,cached_spec_path/use_cached_spec_when_present, etc.). -
Open the UI at
/{swagger.url}(default:/swagger/documentation), e.g.https://your-app.test/swagger/documentation. -
The issues page route is configured via
swagger.issues_url(default:/swagger/issues). -
Describe a route:
Route::get('user', [UserController::class, 'index'])->description('Get list of users with pagination.');
-
Short summary on the route:
Route::get('user', [UserController::class, 'index'])->summary('get users.');
-
Hide an endpoint from the docs:
Route::get('user', [UserController::class, 'index'])->hiddenDoc();
-
Controller section description:
<?php namespace App\Http\Controllers; use G4T\Swagger\Attributes\SwaggerSection; #[SwaggerSection('everything about your users')] class UserController extends Controller { // ... }
-
Documentation basic auth (
config/swagger.php):"enable_auth" => false, "username" => "admin", "password" => "pass", "sesson_ttl" => 100000,
Artisan commands
| Command | What it does |
|---|---|
make:swagger |
Writes OpenAPI JSON to public/{cached_spec_path} (default doc.json). Legacy name; same destination as swagger:cache. |
swagger:cache |
Regenerates and writes the cached spec to public/{cached_spec_path}, or swagger:cache --clear removes that file. Skips generation when swagger.enable is false (clear still works). |
swagger:mock-server |
Optional G4T mock workflow (experimental). |
php artisan make:swagger php artisan swagger:cache php artisan swagger:cache --clear php artisan swagger:mock-server
Mock server (experimental)
G4T provides an optional command that connects your auto-generated API documentation to a hosted mock experience run by G4T. The goal is to make it easier to explore or demo your API surface while you build—without you having to host that side yourself.
Experimental: This feature is under testing. It may change, pause, or evolve without much advance notice. Do not rely on it for business-critical production paths yet.
Run:
php artisan swagger:mock-server
OpenAPI export & caching
make:swagger
Regenerates the spec from the current application routes and writes:
- Path:
public_path(config('swagger.cached_spec_path', 'doc.json')) - Encoding:
JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE
php artisan make:swagger
Run after changing routes, controllers, FormRequests, or resources when you rely on a cached file for the JSON endpoint.
swagger:cache
Same write target and encoding as make:swagger, with an extra clear option:
php artisan swagger:cache php artisan swagger:cache --clear
Use --clear to delete the cached file without regenerating. Regeneration requires swagger.enable to be true.
JSON endpoint behavior
The /{swagger.url}/json response is resolved by DocumentationController::resolveSwaggerSpecification():
load_from_json |
Behavior |
|---|---|
true |
Only the file at public/{cached_spec_path} is used; missing/invalid file → empty array []. |
false |
If use_cached_spec_when_present is true (default) and the file exists with valid JSON, that file is served; otherwise the spec is built live from routes. |
Config / env (defaults apply if keys are missing from an older published config):
cached_spec_path— path underpublic/(defaultdoc.json; envSWAGGER_CACHED_SPEC_PATH).use_cached_spec_when_present— defaulttrue; envSWAGGER_USE_CACHED_SPEC=falseforces live generation without deleting the file.load_from_json— static-file-only mode (legacy).
Suggestions
Feature requests: Canny board.
Contributing
Issues and pull requests are welcome on GitHub.
License
Open-source under the MIT license.
Credits
Maintained by HusseinAlaa.

