mezatsong/laravel-swagger-docs
Composer 安装命令:
composer require mezatsong/laravel-swagger-docs
包简介
Swagger docs generator for Laravel api
关键字:
README 文档
README
Simple to use OAS3 compatible documentation generator.
Also includes Swagger UI.
About
This package is heavily inspired by the darki73/laravel-swagger and kevupton/laravel-swagger.
Usage is pretty similar to the mtrajano/laravel-swagger with the difference being:
- OAS3 support
- Custom decorators
- Custom responses
- Automatic generation (assuming relevant configuration option is turned on)
- Inclusion of Swagger UI
- Models generations
- Generate operation tags based on route prefix or controller's name
Installation
Install package through composer
composer require mezatsong/laravel-swagger-docs
Publish configuration files and views
php artisan vendor:publish --provider "Mezatsong\SwaggerDocs\SwaggerServiceProvider"
Edit the swagger.php configuration file for your liking
Usage
Laravel Swagger Docs works based on recommended practices by Laravel. It will parse your routes and generate a path object for each one. If you inject Form Request classes in your controller's actions as request validation, it will also generate the parameters for each request that has them. For the parameters, it will take into account wether the request is a GET/HEAD/DELETE or a POST/PUT/PATCH request and make its best guess as to the type of parameter object it should generate. It will also generate the path parameters if your route contains them. Finally, this package will also scan any documentation you have in your action methods and add it as summary and description to that path, along with any appropriate annotations such as @deprecated.
One thing to note is this library leans on being explicit. It will choose to include keys even if they have a default. For example it chooses to say a route has a deprecated value of false rather than leaving it out. I believe this makes reading the documentation easier by not leaving important information out. The file can be easily cleaned up afterwards if the user chooses to leave out the defaults.
Command line
Generating the swagger documentation is easy, simply run php artisan swagger:generate in your project root. The output of the command will be stored in your storage path linked in config file.
If you wish to generate docs for a subset of your routes, you can pass a filter using --filter, for example: php artisan swagger:generate --filter="/api"
You can also configure your swagger.php file to always generate schema when accessing Swagger UI or just by adding this line in your .env: SWAGGER_GENERATE_ALWAYS=true
By default, laravel-swagger prints out the documentation in json format, if you want it in YAML format you can override the format using the --format flag. Make sure to have the yaml extension installed if you choose to do so.
Format options are:
json
yaml
@Request() decorator
You can have only one @Request() decorator.
/** * You can also do this, first line will be "summary" * * And anything 1 * apart from the "summary" will count as "description" * * @Request({ * summary: Title of the route, * description: This is a longer description for the route which will be visible once the panel is expanded, * tags: Authentication,Users * }) */ public function someMethod(Request $request) {}
@Response() decorator
You can have multiple @Response decorators
- The
codeproperty is required and must be the first in propery - You can use the optional
descriptionproperty to desscribe your response - You can use the optional
refproperty to refer a model, you can also wrap that model in [] to refer an array of that model or use the full model path inside, finally you can use a schema builder
/** * @Response({ * code: 200 * description: return user model * ref: User * }) * @Response({ * code: 400 * description: Bad Request, array of APIError model * ref: [APIError] * }) * @Response({ * code: 302 * description: Redirect * }) * @Response({ * code: 500 * description: Internal Server Error * }) */ public function someMethod(Request $request) {} /** * You can also refer object directly * * * @Response({ * code: 200 * description: direct user model reference * ref: #/components/schemas/User * }) */ public function someMethod2(Request $request) {} /** * Using P schema builder for Laravel Pagination * * @Response({ * code: 200 * description: a laravel pagination instance with User model * ref: P(User) * }) */ public function someMethod3(Request $request) {}
Note: You can see all available schema builder or create your own schema builder, explore swagger.schema_builders config for more informations.
Custom Validators
These validators are made purely for visual purposes, however, some of them can actually do validation
swagger_default
$rules = [ 'locale' => 'swagger_default:en_GB' ];
swagger_min
$rules = [ 'page' => 'swagger_default:1|swagger_min:1', // This will simply display the 'minimum' value in the documentation 'page' => 'swagger_default:1|swagger_min:1:fail' // This will also fail if the `page` parameter will be less than 1 ];
swagger_max
$rules = [ 'take' => 'swagger_default:1|swagger_min:1|swagger_max:50', // This will simply display the 'maximum' value in the documentation 'take' => 'swagger_default:1|swagger_min:1|swagger_max:50:fail' // This will also fail if the `take` parameter will be greater than 50 ];
mezatsong/laravel-swagger-docs 适用场景与选型建议
mezatsong/laravel-swagger-docs 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 60.88k 次下载、GitHub Stars 达 24, 最近一次更新时间为 2021 年 05 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「generator」 「docs」 「laravel」 「swagger」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mezatsong/laravel-swagger-docs 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mezatsong/laravel-swagger-docs 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mezatsong/laravel-swagger-docs 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Memio's PrettyPrinter, used to generate PHP code from given Model
Bookdown.io With Bootswatch Styles And Prism Syntax Highlighting
Api documentation generator for Laravel 5
Replacement for Swagger. It is built for Laravel
Laravel API documentation generating tool
The StyleCI Docs
统计信息
- 总下载量: 60.88k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 24
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-05-09