PHP   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了swagger在项目中的使用(php)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

为什么要使用swagger:

  1. 利于代码与注释与api文档相同步
  2. 接口的文档在线自动生成
  3. 可以直接使用可视化api文档进行接口调试
  4. 直接生成api文档文件便于保存以及之后统一管理
  5. 支持多种语言java,go,PHP都可以使用swagger生成api文档

首先需要安装以下一个东西:@H_419_15@

1. PHPStorm 插件

1. Swagger Plugin
2. PHP Annotation  

==安装完成PHPStorm扩展后需要重启PHPStorm==

2. 安装swagger-PHP(yaml文件生成工具)

composer require zinco/swagger-PHP

安装完成之后,在PHP文件中写入注释

/**
 * @OA\Info(title="My First API", version="0.1")
 */

/**
 * @OA\Get(
 *     path="/api/resource.json",
 *     @OA\Response(response="200", description="An example resource")
 * )
 */

在另一个文件方法中写入如下代码

require("vendor/autoload.PHP"); // 如果已经实现composer自动加载可将此行注释
$openapi = \OpenApi\scan('/path/to/project');// /path/to/project 为含有swagger注释文件的上层目录路径
header('Content-Type: application/x-yaml');
echo $openapi->toYaml();

访问该文件方法即可获得swagger yaml文件

3. 安装swagger-ui(swagger-api页面文件

composer require swagger-api/swagger-ui

将swagger-ui/dist目录复制到可访问的目录中,如public下
修改index.html中如下位置代码

SwaggerUIBundle({
        url: "http://local.homestead.com/api/swagger", // 需要将此路径改为上一个文件的地址

访问 地址/dist 即可查看api文档

4. swagger 注解格式示例:

/**
 * @OA\Info(title="My First API", version="0.1")
 */

// 读取
/**
 * @Get(
 *     path="/Hello/{iD}",
 *     @Parameter(
 *       name="id",
 *       in="path",
 *       required=true,
 *       description="123123",
 *       @scheR_214_11845@a(type="Integer")
 *     ), 
 *     @Response(
 *         response=200,
 *         description="succesS/成功",
 *         @mediaType(
 *             mediaType="application/json",
 *             @scheR_214_11845@a(
 *              @Property(property="code", type="Integer", format="int32", description="标识"),
 *              @Property(property="msg", type="String", format="int32", description="描述"),
 *              @Property(property="data",type="object",description="返回数据",
 *                 @Property(property="no",type="String",description="版本号"),
 *                 @Property(property="account",type="String",description="用户"),
 *                 @Property(property="real_name",type="String",description="权限名称"),
 *             ),
 *         ),
 *         example={"code": 0,"msg": "success","data": {"no": "1.3","account": "admin","real_name": "god"}}
 *        )
 *     )
 * )
 */
 
 // 增加
/**
 * @Post(
 *     path="/Hello/file-upload",
 *     tags={"admin-member"},
 *     sumMary="Upload one user document",
 *     description="Upload one user document",
 *     @requestBody(
 *      @mediaType(mediaType="application/x-www-form-urlencoded",
 *          @scheR_214_11845@a(
 *              type="object",
 *              required={"file", "id", "type"},
 *              @Property(property="file", type="String", format="binary", description="user document file"),
 *              @Property(property="id", type="Integer", description="user id"),
 *              @Property(property="type", type="String", enum={"verification_file","id_card_file","credit_card_file"})
 *          )
 *      )),
 *     @Response(
 *          response=200,
 *          description="successful operation"
 *     )
 * )
 */
 
/**
 * @Post(
 *     path="/Hello/xx-yy",
 *     tags={"admin-SALEs-type"},
 *     sumMary="Store a newly created SALEs type item in storage",
 *     description="Store a newly created SALEs type item in storage",
 *     @requestBody(required=true, @JsonContent(
 *           required={"SALEs_name", "handle_fee", "commission", "status", "visible", "keywords", "SALEs_name_abbr", "charge_full_domestic", "default", "tiers"},
 *           @Property(property="SALEs_name", type="String", description="SALEs name"),
 *           @Property(property="handle_fee", type="number", format="float", description="handle fee", example="15.00"),
 *           @Property(property="status", type="Integer", enum={1, 0}),
 *           @Property(property="charge_full_domestic", type="Integer", description="charge full domestic"),
 *           @Property(property="default", type="String", enum={1, 0}),
 *           @Property(property="tiers", type="array", description="tiers",
 *              @Items(
 *                  @Property(property="type", type="String", enum={"flat", "basic", "subtract", "platform"}),
 *                  @Property(property="from", type="Integer", description="from", example="0")
 *              )
 *          ),
 *     )),
 *     @Response(
 *          response="200",
 *          description="successful operation"
 *      )
 * )
 */
 
 // 修改
/**
 * @Patch(
 *     path="/admin/member/{iD}",
 *     tags={"admin-member"},
 *     sumMary="get member info",
 *     description="get member info",
 *     @requestBody(required=true, @JsonContent(
 *           @Property(property="name", type="String", description="user name"),
 *           @Property(property="display_currency", type="String", enum={"USD","JPY"})
 *     )),
 *     @Parameter(
 *      name="id", in="path", description="member id",
 *      required=true, @scheR_214_11845@a(type="Integer")),
 *     @Response(
 *          response=200,
 *          description="successful operation"
 *     )
 * )
 */
 
 // 删除
/**
 * @delete(
 *     path="/Hello/delete/{iD}",
 *     tags={"admin-member"},
 *     sumMary="Remove the specified resource from storage",
 *     description="Remove the specified resource from storage",
 *     @Parameter(
 *      name="id", in="path", description="member id",
 *      required=true, @scheR_214_11845@a(type="Integer")),
 *     @Response(
 *          response=200,
 *          description="successful operation"
 *     )
 * )
 */
 
 // 加密认证
 /**
  * @SecurityscheR_214_11845@e(
  *     type="http",
  *     description="xxx sign",
  *     in="header",
  *     scheR_214_11845@e="bearer"
  * )
  */
 
 设置公共头
 /**
 *@Server(url="http://local.publichomestead.com/Hello",description="asdfasdasdf")
 */

还以一些语法会陆续进行补充@H_419_15@

作者:ReallyWang
链接https://www.jianshu.com/p/599fa5f2b915
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

大佬总结

以上是大佬教程为你收集整理的swagger在项目中的使用(php)全部内容,希望文章能够帮你解决swagger在项目中的使用(php)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: