Table of Contents

Swagger 文档管理

官方网站:https://swagger.io/

快速编写你的 RESTFUL API 接口文档工具,通过注释定义接口和模型,可以和代码文件放置一起,也可以单独文件存放。

优势

  • 通过代码注解定义文档,更容易保持代码文档的一致性
  • 模型复用,减少文档冗余,带来更可靠的文档
  • 提供客户端访问接口,可以直接调试接口,不需要第三方工具进行调用测试接口
  • 支持权限认证,等功能

Laravel Swagger 扩展

扩展源代码地址:https://github.com/DarkaOnLine/L5-Swagger

1
2
composer require darkaonline/l5-swagger
php artisan vendor:publish --provider "L5Swagger\L5SwaggerServiceProvider"

将会生成配置文件 l5-swagger.php,其中需要注意的配置项

  • routes.api 默认值为 api/documentation Swagger 文档系统访问路由
  • routes.docs Swagger 解析注释生成文档 JSON 文件的存储路径
  • paths.annotations 有效注释的路径配置,默认 base_path('app') 应用路径
  • generate_always 测试环境应该开启这个配置,每次访问都会自动解析注释生成最新的文档
  • swagger_version 这个默认为 2.0 , 建议修改为 3.0

下文所有的注释内容,需要存在于 paths.annotations 路径下。

接口版本

@OA\Info 定义接口版本,标题,描述,已经作者信息。

1
2
3
4
5
6
7
8
9
10
11
/**
* @OA\Info(
* version="1.0",
* title="用户管理",
* description="用户模块接口",
* @OA\Contact(
* name="PHP 开发支持",
* email="dreamhelingfeng@gmail.com"
* )
* )
*/

接口请求方式与请求路径

@OA\Get,@OA\Post 定义接口请求方式

示例:根据USER_ID请求用户详情信息 /api/users/{user_id}
接口分组配置:tags,将会对接口归类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* @OA\Get(
* path="/api/users/{user_id}",
* summary="根据 ID 获取用户信息",
* tags={"用户管理"},
* @OA\Parameter(
* name="user_id",
* in="path",
* required=true,
* description="用户 ID",
* @OA\Schema(
* type="string"
* )
* ),
* @OA\Response(
* response=200,
* description="用户对象",
* @OA\JsonContent(ref="#/components/schemas/UserModel")
* )
* )
*/

接口请求参数

通过 Swagger 可以定义三类参数,path,header,query

  • path, 参数存在 endponit 中,如,/users/{user_id}
  • header, 参数存在请求头部,如,token 用户校验令牌
  • query, 请求参数带在请求URL上,如,/users?nickname={nickname}
1
2
3
4
5
6
7
8
9
10
11
12
/**
* @OA\Get(
* @OA\Parameter(
* name="user_id",
* in="path",
* required=true,
* description="用户 ID",
* @OA\Schema(
* type="string"
* )
* )
*/

POST 提交表单,通常使用 application/json 方式,如,创建用户

@OA\Post
path=/users

1
2
3
4
5
6
7
8
9
10
11
/**
* @OA\RequestBody(
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(ref="#/components/schemas/UserModel"),
* example={
* "username": "helingfeng", "age": "25"
* }
* )
* )
*/

Schema 模型定义

上面的注释中,多次引用 @OA\Schema(ref="#/components/schemas/UserModel")

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* @OA\Schema(
* schema="UserModel",
* required={"username", "age"},
* @OA\Property(
* property="username",
* type="string",
* description="用户名称"
* ),
* @OA\Property(
* property="age",
* type="int",
* description="年龄"
* )
* )
*/

Laravel-Swagger 会自动根据您的注释进行匹配

上传文件接口示例

定义一个接口,接收上传文件,并返回结果远程文件地址

接口定义:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* @OA\Post(
* path="/api/files/upload",
* summary="文件上传",
* tags={"文件上传"},
* @OA\RequestBody(
* @OA\MediaType(
* mediaType="multipart/form-data",
* @OA\Schema(
* required={"upload_file"},
* @OA\Property(
* property="upload_file",
* type="file",
* description="上传文件"
* ),
* ),
* )
* ),
* @OA\Response(
* response=200,
* description="上传成功",
* @OA\JsonContent(ref="#/components/schemas/UploadFileModel")
* ),
* @OA\Response(
* response="default",
* description="error",
* @OA\JsonContent(ref="#/components/schemas/ErrorModel")
* )
* )
*/

模型定义:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* @OA\Schema(
* schema="UploadFileModel",
* @OA\Property(
* property="file_name",
* type="string",
* description="文件名,不包含路径"
* ),
* @OA\Property(
* property="file_path",
* type="string",
* description="文件路径"
* ),
* @OA\Property(
* property="file_url",
* type="string",
* description="URL链接,用于展示"
* ),
* @OA\Property(
* property="file_size",
* type="string",
* description="文件大小,单位B"
* ),
* @OA\Property(
* property="extension",
* type="string",
* description="文件扩展名"
* )
* )
*/

访问 api/documentation 效果如图

try it out 上传文件操作结果

需要权限认证的接口

一般对外网开放的接口,需要添加权限控制,Swagger 提供很好的支持

在 l5-swagger.php 配置文件中添加, crypt-token 定义请求头部必须添加 token 作为权限校验令牌。

1
2
3
4
5
6
7
8
9
10
11
12
13
'security' => [
/*
|--------------------------------------------------------------------------
| Examples of Security definitions
|--------------------------------------------------------------------------
*/
'crypt-token' => [ // Unique name of security
'type' => 'apiKey', // The type of the security scheme. Valid values are "basic", "apiKey" or "oauth2".
'description' => 'A short description for security scheme',
'name' => 'token', // The name of the header or query parameter to be used.
'in' => 'header', // The location of the API key. Valid values are "query" or "header".
],
]

接口注释中,添加对应的验证方式:

1
2
3
4
5
/**
* security={
* {"crypt-token": {}}
* },
*/

更多 Swagger3 定义字段描述,可以查看官方文档:https://swagger.io/specification/#parameterObject

使用 Laravel-Swagger 编写接口文档(php)的更多相关文章

  1. Spring Boot 集成 Swagger 构建接口文档

    在应用开发过程中经常需要对其他应用或者客户端提供 RESTful API 接口,尤其是在版本快速迭代的开发过程中,修改接口的同时还需要同步修改对应的接口文档,这使我们总是做着重复的工作,并且如果忘记修 ...

  2. .net core 使用 swagger 生成接口文档

    微软参考文档:https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger?tabs= ...

  3. Go语言使用swagger生成接口文档

    swagger介绍 Swagger本质上是一种用于描述使用JSON表示的RESTful API的接口描述语言.Swagger与一组开源软件工具一起使用,以设计.构建.记录和使用RESTful Web服 ...

  4. 作为Java开发工程师,如何高效优雅地编写接口文档

    作为一名优秀的Java开发工程师,编写接口文档向来是一件很头疼的事情.本来就被bug纠缠的很累了,你还让我干这? 其实,你可以试试ApiPost. ApiPost的定位是Postman+Swagger ...

  5. asp.net core 使用 swagger 生成接口文档

    参考地址:http://www.cnblogs.com/daxnet/p/6181366.html http://www.jianshu.com/p/fa5a9b76f3ed 微软参考文档:https ...

  6. webapi 利用webapiHelp和swagger生成接口文档

    webapi 利用webapiHelp和swagger生成接口文档.均依赖xml(需允许项目生成注释xml) webapiHelp:微软技术自带,仅含有模块.方法.请求-相应参数的注释. swagge ...

  7. 基于swagger进行接口文档的编写

    0. 前言 近期忙于和各个银行的代收接口联调,根据遇到的问题,对之前编写的接口进行了修改,需求收集和设计接口时想到了方方面面,生产环境下还是会遇到意想不到的问题,好在基本的执行逻辑已确定,因此只是对接 ...

  8. 用Swagger生成接口文档

    Swagger简介 在系统设计的时候,各个应用之间往往是通过接口进行交互的.因此接口的定义在整个团队中就变得尤为重要.我们可以把接口的规范用接口描述语言进行描述,然后Swagger可以根据我们定义的接 ...

  9. Spring Boot 集成 Swagger生成接口文档

    目的: Swagger是什么 Swagger的优点 Swagger的使用 Swagger是什么 官网(https://swagger.io/) Swagger 是一个规范和完整的框架,用于生成.描述. ...

随机推荐

  1. less使用手记 主题切换 全局import less

    实现主题颜色切换 components/theme.less,跟据@theme读取主题布局 @theme: dark; .dark-theme (@transparency) when (@theme ...

  2. SqlServer中的更新锁(UPDLOCK和READPAST)

    UPDLOCK和READPAST,通过UPDLOCK和READPAST的结合我们能够解决许多问题,比如我当前项目中对于更新预约人数,则用到了UPDLOCK和READPAST,因为考虑到并发如果固定预约 ...

  3. 目标检测论文解读4——Faster R-CNN

    背景 Fast R-CNN中的region proposal阶段所采用的SS算法成为了检测网络的速度瓶颈,本文是在Fast R-CNN基础上采用RPN(Region Proposal Networks ...

  4. Linux 定时任务的控制台导出导入到文件

    这个不但适用于定时任务,也适用于一般的脚本. 以前喜欢用>>这样的快捷方式, 今天看到用tee命令的,也记下. 55 23 * * * /bin/sh /sql_bak/restore_s ...

  5. 运行 npm run lint -- --fix,提示:error Use the global form of 'use strict'

    运行 npm run lint -- --fix,提示:error Use the global form of 'use strict',使用说明网址:https://eslint.org/docs ...

  6. 在 Less 中使用 calc() 的坑:height: calc(~"50% - 21px");

    注意点: 中间的运算符两头都要有空格 写法(加上~符号):height: calc(~"50% - 21px"); 出处:https://mp.weixin.qq.com/s/CY ...

  7. PHP 验证Email的函数

    <?php   function validateEmail($email) {    $isValid = true;    $atIndex = strrpos($email, " ...

  8. Oracle逻辑导入数据(IMP/IMPDP)

    使用IMPDP导入数据的前提是数据是使用EMPDP导出的,同样也是在DOS窗口下直接输入IMPDP和登录数据库的用户名,即可导人数据. impdp导到指定用户下: impdp student/1234 ...

  9. 运行银联支付系统demo

    1.Demo下载 下载地址:https://open.unionpay.com/tjweb/acproduct/list?apiservId=448 2.导入项目 注意:非maven项目,在导入ide ...

  10. 【转】FIddler+Proxifer工具对windows PC客户端进行抓包

    开篇:要想实现写爬虫,抓取到数据,首先我们应该分析客户端和服务器的请求/响应,前提就是我们能监控到客户端是如何与服务器交互的,下面来记录下常见的三种情况下的抓包方法 1.PC端浏览器网页抓包网页板抓包 ...