ronasit/laravel-swagger
Composer 安装命令:
composer require ronasit/laravel-swagger
包简介
Provided middleware for generating of swagger-documentation file by run testing of restful API.
README 文档
README
Laravel Swagger plugin
Comparison to another documentation generators
| LaravelSwagger | Scramble | |
|---|---|---|
| Force developers to write tests | ✅ | ❌ |
| Guarantee that API works | ✅ | ❌ |
| Works with any route types covered by tests | ✅ | ❌ |
| Generate response schema using JSON Resource class | ❌ | ✅ |
| Runtime documentation generation | ❌ | ✅ |
Introduction
This plugin is designed to generate documentation for your REST API during the passing PHPUnit tests.
Installation
- Install the package using the following command:
composer require ronasit/laravel-swagger
Note
For Laravel 5.5 or later the package will be auto-discovered. For older versions add the
AutoDocServiceProviderto the providers array inconfig/app.phpas follow:'providers' => [ ... RonasIT\AutoDoc\AutoDocServiceProvider::class, ],
- Run
php artisan vendor:publish --provider=RonasIT\\AutoDoc\\AutoDocServiceProvider
- Add
\RonasIT\AutoDoc\Http\Middleware\AutoDocMiddleware::classmiddleware to the global HTTP middleware listbootstrap\app.php:
return Application::configure(basePath: dirname(__DIR__)) ->withMiddleware(function (Middleware $middleware) { $middleware->use([ ... \RonasIT\AutoDoc\Http\Middleware\AutoDocMiddleware::class, ]); });
-
Add
\RonasIT\AutoDoc\Traits\AutoDocTestCaseTraittrait totests/TestCase.php -
Configure documentation saving using one of the next ways:
- Add
SwaggerExtensionto the<extensions>block of yourphpunit.xml. Please note that this way will be removed after updating PHPUnit up to 10 version (sebastianbergmann/phpunit#4676)
<phpunit> <extensions> <bootstrap class="RonasIT\AutoDoc\Support\PHPUnit\Extensions\SwaggerExtension"/> </extensions> <testsuites> <testsuite name="Feature"> <directory suffix="Test.php">./tests/Feature</directory> </testsuite> </testsuites> </phpunit>
- Call
php artisan swagger:push-documentationconsole command after thetestsstage in your CI/CD configuration
Usage
Basic usage
-
Create request class:
<?php namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; /** * @summary Update user * * @deprecated * * @description * This request should be used for updating the user data * * @_204 Successful * * @is_active will indicate whether the user is active or not */ class UpdateUserDataRequest extends FormRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true; } /** * Validation Rules * * @return array */ public function rules() { return [ 'name' => 'string', 'is_active' => 'boolean', 'age' => 'integer|nullable' ]; } }
Note
For correct working of plugin you'll have to dispose all the validation rules in the
rules()method of your request class. Also, your request class must be connected to the controller via dependency injection. Plugin will take validation rules from the request class and generate fields description of input parameter. -
Create a controller and a method for your route:
<?php namespace App\Http\Controllers; use App\Http\Requests\Users\UpdateUserDataRequest; class UserController extends Controller { public function update(UpdateUserDataRequest $request, UserService $service, $id) { // do something here... return response('', Response::HTTP_NO_CONTENT); } }
Note
Dependency injection of request class is optional but if it not presents, the "Parameters" block in the API documentation will be empty.
-
Create test for API endpoint:
public function testUpdate() { $response = $this->json('put', '/users/1', [ 'name': 'Updated User', 'is_active': true, 'age': 22 ]); $response->assertStatus(Response::HTTP_NO_CONTENT); }
-
Run tests
-
Go to route defined in the
auto-doc.routeconfig
Annotations
You can use the following annotations in your request classes to customize documentation of your API endpoints:
- @summary - short description of request
- @description - implementation notes
- @_204 - custom description of response code. You can specify any code as you want.
- @some_field - description of the field from the rules method
- @deprecated - mark route as deprecated
Note
If you do not use request class, the summary and description and parameters will be empty.
Configs
auto-doc.route- route for generated documentationauto-doc.basePath- root of your API
Custom driver
You can specify the way to collect and view documentation by creating your own custom driver.
You can find example of drivers here.
Viewing OpenAPI documentation
As of version 2.2, the package includes the ability to switch between OpenAPI documentation
viewers. To access different viewers, modify the documentation_viewer configuration.
This change is reflected immediately, without the need to rebuild the documentation file.
Merging additional documentations
The package supports the integration of the primary documentation with additional valid
OpenAPI files specified in the additional_paths configuration.
Integration with Postman
The package provides a separate endpoint to get the OpenAPI spec documentation in a Postman-specific format. Pass your app's /auto-doc/documentation/postman endpoint into the Postman import.
Migration guides
Contributing
Thank you for considering contributing to Laravel Swagger plugin! The contribution guide can be found in the Contributing guide.
License
Laravel Swagger plugin is open-sourced software licensed under the MIT license.
ronasit/laravel-swagger 适用场景与选型建议
ronasit/laravel-swagger 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 130.98k 次下载、GitHub Stars 达 179, 最近一次更新时间为 2016 年 08 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「swagger」 「rest-api」 「auto-documentation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ronasit/laravel-swagger 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ronasit/laravel-swagger 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ronasit/laravel-swagger 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Provided middleware for generating of swagger-documentation file by run testing of restful API.
A lightweight, secure-by-default PHP microframework built on Slim – providing Laravel-like features (ORM, authentication, migrations, caching) without the bloat. Perfect for building REST APIs and small-to-medium PHP applications.
Swagger integration to Laravel 5
Easy Swagger endpoint using the Zend Expressive routes config
一个接口文档生成器
Get wordpress nav menus and share the main site menus with the child sites.
统计信息
- 总下载量: 130.98k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 180
- 点击次数: 26
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-08-29
