Swagger Document Management
Official Website: https://swagger.io/
Fast write your RESTFUL API
interface documentation tool, through annotations defined interfaces and models, and code files can be placed together or separately file storage.
Advantage
- ?? By definition document annotation of code, the code easier to maintain the consistency of the document
- Model reuse, reduce redundant documentation, more reliable documents
- Provides client access interface, you can debug interface directly, without third-party tools to test interface call
- Support certification authority, and other functions
Laravel Swagger extension
Extended source address: https://github.com/DarkaOnLine/L5-Swagger
1 |
composer require darkaonline/l5-swagger |
It will generate a configuration file l5-swagger.php
, which should be noted configuration items
routes.api
The default valueapi/documentation
Swagger document routing system accessroutes.docs
Swagger parsing JSON document storage path file generated commentspaths.annotations
Effective annotations path configuration, the defaultbase_path('app')
application pathgenerate_always
Test environment should enable this configuration, each visit will automatically parse the latest documentation generated commentsswagger_version
The default is 2.0, the proposed revised to 3.0
Hereinafter all annotation content, a need exists in paths.annotations
the path.
Interface Version
@OA\Info
Defined interface version, title, description, author information already.
1 |
/ ** |
Path request interface to request a manner
@OA\Get
, @OA\Post
Define the interface mode request
Example: The USER_ID
requesting user before information /api/users/{user_id}
Interface grouping configuration: tags
will be classified on the interface
1 |
/ ** |
Request Interface parameters
Can be defined by three parameters Swagger, path
, header
,query
- path, in the presence of endponit parameters, such as, / users / {user_id}
- header, there is a request header parameter, such as, a user token verification token
- query, with the request parameters in the request URL, as, / users? nickname = {nickname}
1 |
/** |
POST submit the form, commonly used application/json
methods, such as creating user
@OA\Post
path=/users
1 |
/** |
Schema 模型定义
上面的注释中,多次引用 @OA\Schema(ref="#/components/schemas/UserModel")
1 |
/** |
Laravel-Swagger 会自动根据您的注释进行匹配
上传文件接口示例
定义一个接口,接收上传文件,并返回结果远程文件地址
接口定义:
1 |
/** |
模型定义:
1 |
/** |
访问 api/documentation
效果如图
try it out
上传文件操作结果
需要权限认证的接口
一般对外网开放的接口,需要添加权限控制,Swagger 提供很好的支持
在 l5-swagger.php
配置文件中添加, crypt-token
定义请求头部必须添加 token
作为权限校验令牌。
1 |
'security' => [ |
接口注释中,添加对应的验证方式:
1 |
/** |
更多 Swagger3 定义字段描述,可以查看官方文档:https://swagger.io/specification/#parameterObject