php swagger安装和使用教程

1 篇文章 0 订阅

通过composer进行安装,命令:        

composer require zircote/swagger-php:2.*

代码需要引入

use Swagger\Annotations as SWG;

默认必须都要加上:

/**
 * @SWG\Swagger(
 *      @SWG\Info(
 *          title="测试接口",
 *          version="1.0.0"
 *      )
 * )
 */

post请求加Authorization认证:

/**
     * @SWG\Post(
     *     path="/api/xxxx",
     *     tags={"xxxx"},
     *     summary="xxxxx",
     *     @SWG\Parameter(
     *          in="header",
     *          name="Authorization",
     *          description="token",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\SecurityScheme(
     *         securityDefinition="api_key",
     *         type="apiKey",
     *         in="header",
     *         description="token",
     *         name="Authorization"
     *     ),
     *  @SWG\Response(
     *      response=200,
     *      description="http状态码",
     *       @SWG\Schema(
     *          @SWG\Property(
     *                  property="code",
     *                  type="number",
     *                  description="状态码"
     *           ),
     *           @SWG\Property(
     *                  property="msg",
     *                  type="string",
     *                  description="错误提示"
     *           ),
     *            @SWG\Property(
     *                  property="data",
     *                  type="array",
     *                  items={},
     *                  description="返回的数据"
     *           )
     *      )
     *  )
     * 
     * )
     */

post请求:

h="/api/xxxxx",
     *  tags={"xxxxxx"},
     *  summary="用户登录",
     *  produces={"application/json"},
     *  @SWG\Parameter(
     *      in="formData",
     *      name="name",
     *      description="账号",
     *      type="string",
     *      required = true,
     *  ),
     *  @SWG\Parameter(
     *      in="formData",
     *       name="password",
     *      description="密码",
     *      type="string",
     *      required = true,
     *  ),
     *  @SWG\Response(
     *      response=200,
     *      description="http状态码",
     *       @SWG\Schema(
     *          @SWG\Property(
     *                  property="code",
     *                  type="number",
     *                  description="状态码"
     *           ),
     *           @SWG\Property(
     *                  property="msg",
     *                  type="string",
     *                  description="错误提示"
     *           ),
     *            @SWG\Property(
     *                  property="data",
     *                  type="array",
     *                  items={},
     *                  description="返回的数据"
     *           )
     *      )
     *  )
     * 
     * )
     */

Post Json请求:

 /**
     * @SWG\Post(
     *  path="/api/xxxx",
     *  tags={"xxxx"},
     *  summary="xxxxxxx",
     *  produces={"application/json"},
     *  @SWG\Parameter(
     *      in="body",
     *      required=true,
     *      name="body",
     *      description="请求参数",
     *      @SWG\Schema(
     *          @SWG\Property(
     *              property="name",
     *              type="string",
     *              description="账号"
     *          ),
     *          @SWG\Property(
     *              property="password",
     *              type="string",
     *              description="密码"
     *          ),
     *	     @SWG\Property(
     *              property="role_ids",
     *              type="array",
     *              items={1},
     *              description="角色ID数组元素"
     *          ),
     *      )
     *  ),
     *  @SWG\Response(
     *      response=200,
     *      description="http状态码",
     *       @SWG\Schema(
     *          @SWG\Property(
     *                  property="code",
     *                  type="number",
     *                  description="状态码"
     *           ),
     *           @SWG\Property(
     *                  property="msg",
     *                  type="string",
     *                  description="错误提示"
     *           ),
     *            @SWG\Property(
     *                  property="data",
     *                  type="array",
     *                  items={},
     *                  description="返回的数据"
     *           )
     *      )
     *  )
     * 
     * )
     */

生存json文件执行代码:

php vendor\zircote\swagger-php\bin\swagger 代码路径 -o 存放路径

还需要下载swagger-ui,然后修改dist目录index.html 里面url路径

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值