g4t/swagger
Composer 安装命令:
composer require g4t/swagger
包简介
g4t laravel swagger
README 文档
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.
Additional resources
g4t/swagger 适用场景与选型建议
g4t/swagger 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 49.44k 次下载、GitHub Stars 达 124, 最近一次更新时间为 2023 年 06 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 g4t/swagger 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 g4t/swagger 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 49.44k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 125
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-06-07

