Swagger框架 API文檔速成

Swagger 斯瓦格 / 絲襪哥

SwaggerHP_Build.png

Swagger中式漢語(yǔ):
絲襪哥? 斯瓦格? 有沒(méi)有很熟悉,當(dāng)然不是你想的 格瓦斯 那是飲料!
Swagger給人第一印象就是【最(hen)流(niu)行(bai)】;

我們?yōu)槭裁匆肧wagger文檔?
1.自動(dòng)化文檔,代替手寫(xiě)API的web工具,代替MarkDown、world文檔、還有比較low的微信講述哈哈
2.配合swagger-ui我們只需要把參數(shù)寫(xiě)在注釋里面就生成了文檔
3.在線動(dòng)態(tài)測(cè)試API接口,相當(dāng)于postman一樣調(diào)用接口返回?cái)?shù)據(jù)success/error/data
// 下面有兩種使用方式,都可以使用只要實(shí)現(xiàn)最終展示給前端文檔目的就可以

1. Swagger編輯json使用方法:

Composer 安裝在框架中使用

使用配置到 Laravel 框架中

使用 swagger-ui + swagger-edit
下載 Swagger-ui 只需要 dist 里面的文件放在項(xiàng)目根目錄 public 下并配置上路由使用訪問(wèn)。
下載 Swagger-editor 只要 dist 目錄的東西和根目錄的index.html 按照官方OpenAPI規(guī)范給出的語(yǔ)法使用編輯。

2. 代碼注釋解析使用方法:

phpStorm安裝組件 (安裝組件Swagger PluginPHP Annotations)

第一個(gè)組件CodeGlance是(側(cè)欄代碼地圖)這個(gè)和swagger沒(méi)有關(guān)系
只需要安裝Swagger PluginPHP Annotations,安裝如果超時(shí)提示
Plugin Swagger Plugin was not installed: Cannot download無(wú)法下載:
查看解決方法:CSDN-phpStorm安裝組件超時(shí)

phpStorm組件.png

PHPStorm配置自動(dòng)補(bǔ)全

配置IDE自動(dòng)補(bǔ)全功能
配置IDE自動(dòng)補(bǔ)全.png
配置IDE自動(dòng)補(bǔ)全信息
  • 控制器里注釋用 (Abbreviation:swaggerController Description:swagger)
@SWG\Post(
*     path="/register",
*     tags={"User"},
*     summary="接口名稱",
*     description="接口描述信息",
*     operationId="register-store",
*     produces={"application/json"},
*     @SWG\Parameter(ref="#/parameters/Authorization"),
*     @SWG\Response(
*         response=200,
*         description="注冊(cè)成功",
*         @SWG\Schema(ref="#/definitions/success"),
*     ),
*     @SWG\Response(
*         response=422,
*         description="注冊(cè)失敗",
*     ),
* )
  • Parameter.php使用 (Abbreviation:swaggerParameter Description:swagger)
*******************************paraName
 * @SWG\Parameter(
 *    parameter="輸入框?qū)?yīng)控制器尋找名",
 *    name="輸入框名稱",
 *    description="輸入框描述",
 *    in="header",        // 放在/header/query(鏈接)/formData/body/path
 *    type="string",      // integer/float/string/boolean/object/password(模糊輸入) byte(編碼的字符base64)
 *    format="string",    // integer/float/string/boolean/object/password(模糊輸入) byte(編碼的字符base64)
 *    required=true,      // 輸入框不能為空
 *    enum={"0",0,1},     // 可以加枚舉
 * ),
 * *******************************end
  • 接口詳情描述-first()單數(shù)據(jù)注釋用 (Abbreviation:swaggerDefinition-first Description:swagger)
@SWG\Definition(
*     definition="user-index",
*     type="object",
*     required={"status","code","message","data"},
*     @SWG\Property(
*         property="status",
*         type="boolean",
*         example="true",
*         description="請(qǐng)求狀態(tài)",
*     ),
*     @SWG\Property(
*         property="code",
*         type="integer",
*         example=200,
*         description="狀態(tài)碼",
*     ),
*     @SWG\Property(
*         property="message",
*         type="string",
*         example="操作成功",
*         description="狀態(tài)語(yǔ)義",
*     ),
*     @SWG\Property(
*         property="data",
*         type="object",
*         @SWG\Property(
*             property="id",
*             type="integer",
*             example=1,
*         ),
*         @SWG\Property(
*             property="user",
*             type="array",
*             @SWG\Items(
*                 type="object",
*                 @SWG\Property(
*                     property="user_id",
*                     type="integer",
*                     example=1,
*                 ),
*                 @SWG\Property(
*                     property="username",
*                     type="string",
*                     example="三腳貓",
*                 ),
*             ),
*         ),
*         @SWG\Property(
*             property="hello",
*             type="string",
*             example="helloWorld",
*         ),
*     ),
* ),
  • 接口詳情描述-get()多數(shù)據(jù)注釋用 (Abbreviation:swaggerDefinition-get Description:swagger)
@SWG\Definition(
 *     definition="user-userInfo-index",
 *     type="object",
 *     required={"status","code","message","data"},
 *     allOf={
 *          @SWG\Schema(
 *              required={"data"},
 *              @SWG\Property(
 *                  property="status",
 *                  type="boolean",
 *                  example="true",
 *                  description="請(qǐng)求狀態(tài)",
 *              ),
 *              @SWG\Property(
 *                  property="code",
 *                  type="integer",
 *                  example=200,
 *                  description="狀態(tài)碼",
 *              ),
 *              @SWG\Property(
 *                  property="message",
 *                  type="string",
 *                  example="操作成功",
 *                  description="狀態(tài)語(yǔ)義",
 *              ),
 *              @SWG\Property(
 *                  property="data",
 *                  type="array",
 *                  @SWG\Items(
 *                      type="object",
 *                      required={"user_id"},
 *                      @SWG\Property(
 *                          property="user_id",
 *                          type="integer",
 *                          example=1,
 *                          description="可有可無(wú)的字段描述",
 *                      ),
 *                      @SWG\Property(
 *                          property="username",
 *                          type="string",
 *                          example="三腳貓",
 *                      ),
 *                  ),
 *             ),
 *          )
 *     },
 * ),
  • 接口詳情描述-paginate()分頁(yè)數(shù)據(jù)注釋用 (Abbreviation:swaggerDefinition-get-paginate Description:swagger)
@SWG\Definition(
*     definition="user-update",
*     type="object",
*     required={"status","code","message","data"},
*     allOf={
*         @SWG\Schema(
*             required={"data"},
*             @SWG\Property(
*                 property="status",
*                 type="boolean",
*                 example="true",
*                 description="請(qǐng)求狀態(tài)",
*             ),
*             @SWG\Property(
*                 property="code",
*                 type="integer",
*                 example=200,
*                 description="狀態(tài)碼",
*             ),
*             @SWG\Property(
*                 property="message",
*                 type="string",
*                 example="操作成功",
*                 description="狀態(tài)語(yǔ)義",
*             ),
*             @SWG\Property(
*                 property="data",
*                 type="array",
*                 @SWG\Items(
*                     type="object",
*                     required={"user_id"},
*                     @SWG\Property(
*                         property="user_id",
*                         type="integer",
*                         example=1,
*                         description="可有可無(wú)的字段描述",
*                     ),
*                     @SWG\Property(
*                         property="username",
*                         type="string",
*                         example="三腳貓",
*                     ),
*                 ),
*             ),
*         ),
*         @SWG\Schema(ref="#/definitions/paginate"),
*     },
* ),
  • OAuth 公共使用 (Parameter.php)
 * *********************************token
 * @SWG\Parameter(
 *   parameter="Authorization",
 *   name="Authorization",
 *   description="oauth token",
 *   type="string",
 *   format="string",
 *   in="header",
 *   required=true
 * ),
 * *********************************end
  • Success 公共使用 (Definitions.php)
 * ************************************  success  **********************************
 * @SWG\Definition(
 *     definition="success",
 *     type="object",
 *     required={"status","code","message"},
 *     allOf={
 *          @SWG\Schema(
 *              @SWG\Property(
 *                  property="status",
 *                  type="boolean",
 *                  example="true",
 *                  description="請(qǐng)求狀態(tài)",
 *              ),
 *              @SWG\Property(
 *                  property="code",
 *                  type="integer",
 *                  example=200,
 *                  description="狀態(tài)碼",
 *              ),
 *              @SWG\Property(
 *                  property="message",
 *                  type="string",
 *                  example="操作成功",
 *                  description="狀態(tài)語(yǔ)義",
 *              ),
 *              @SWG\Property(
 *                  property="data",
 *                  type="string",
 *                  example="[]",
 *                  description="無(wú)數(shù)據(jù)返回",
 *              ),
 *          )
 *     },
 * ),
 * *********************************************************end
  • 分頁(yè) 公共使用 (Definitions.php # 引入語(yǔ)法 @SWG\Schema(ref="#/definitions/paginate"),)
 * ************************************  分頁(yè)  **********************************
 * @SWG\Definition(
 *      definition="paginate",
 *      type="object",
 *      required={"links","meta"},
 *      @SWG\Property(
 *          property="links",
 *          type="object",
 *          @SWG\Property(
 *              property="first",
 *              type="string",
 *              example="http://www.xxx.com/api/xxxxx?page=1",
 *          ),
 *          @SWG\Property(
 *              property="last",
 *              type="string",
 *              example="http://www.xxx.com/api/xxxxx?page=1",
 *          ),
 *          @SWG\Property(
 *              property="prev",
 *              type="string",
 *              example="http://www.xxx.com/api/xxxxx?page=1",
 *          ),
 *          @SWG\Property(
 *              property="next",
 *              type="string",
 *              example="http://www.xxx.com/api/xxxxx?page=1",
 *          ),
 *      ),
 *      @SWG\Property(
 *          property="meta",
 *          type="object",
 *          @SWG\Property(
 *              property="current_page",
 *              type="integer",
 *              example=1,
 *          ),
 *          @SWG\Property(
 *              property="from",
 *              type="integer",
 *              example=2,
 *          ),
 *          @SWG\Property(
 *              property="last_page",
 *              type="integer",
 *              example=4,
 *          ),
 *          @SWG\Property(
 *              property="path",
 *              type="string",
 *              example="http://www.xxx.com/api/xxxxx?page=1",
 *          ),
 *          @SWG\Property(
 *              property="per_page",
 *              type="integer",
 *              example="18",
 *          ),
 *          @SWG\Property(
 *              property="to",
 *              type="integer",
 *              example=18,
 *          ),
 *          @SWG\Property(
 *              property="total",
 *              type="integer",
 *              example=5,
 *          ),
 *      ),
 * ),
 * *********************************************************end

解釋Swagger參數(shù)

/**
* 這是一個(gè)資源控制器的Show方法 .
* @param  int $id
* @return \App\Http\Resources\Product\GoodsShowResource
*
* @SWG\Get(
*    path="/goodsNorm/{id}",  //路由名稱
*    tags={"Product"},  //標(biāo)簽名稱 eg: Http/Controller/Document/Product 里面放接口文檔Responses集合
*    operationId="goods-norm",  //用于識(shí)別操作url地址欄中顯示
*    summary="獲取商品",  //外面標(biāo)題
*    description="獲取單個(gè)商品詳細(xì)數(shù)據(jù)",  //里面詳細(xì)標(biāo)題
*    consumes={"application/json"},    //Mime類(lèi)型常用的就這兩種 application/x-www-form-urlencoded
*    @SWG\Parameter(ref="#/parameters/Authorization"),  //可理解為參數(shù)定義路徑/Document/parameters.php
*    @SWG\Parameter(ref="#/parameters/common-id"),      //同上
*    @SWG\Response(                                     //必填的響應(yīng)對(duì)象
*        response=200,                                  //響應(yīng)的狀態(tài)碼
*        description="成功獲單個(gè)商品",                   //響應(yīng)后的提示
*        @SWG\Schema(ref="#/definitions/goods-norm")    //響應(yīng)成功后的參數(shù)示例
*    )
*    @SWG\Response(
*        response=404,
*        description="沒(méi)有找到您要詳細(xì)參數(shù)的數(shù)據(jù)",
*    )
* )
*/
public function show($id){
      //@#$%^&**  業(yè)務(wù)邏輯層

      //API資源返回json數(shù)據(jù)格式 (詳情見(jiàn)Laravel5.5手冊(cè) Eloquent ORM的API資源)
      return new GoodsNormResource($detail);
}

解釋參數(shù)構(gòu)造->Parameter.php

@SWG\Parameter(ref="#/parameters/Authorization")
文件夾路徑:app/Http/Controller/Document/parameters.php

<?php
/**
 ************************************  token  **********************************
 * @SWG\Parameter(
 *   parameter="Authorization",      //Authorization 名稱,用來(lái)在方法注釋尋找
 *   name="Authorization",          //千萬(wàn)別用漢字,如果在傳id的時(shí)候會(huì)解析錯(cuò)誤
 *   description="oauth token",    //inuput輸入框描述
 *   type="string",     //數(shù)據(jù)類(lèi)型 integer string boolean password模糊輸入 float數(shù)字 byte編碼的字符base64
 *   format="string",  //同上,不知道干嘛的,寫(xiě)上錯(cuò)不了- -
 *   in="header",     //
 *   required=true  //輸入框不能為空
 * ),
 ************************************  common  **********************************
 * @SWG\Parameter(
 *    parameter="common-id",
 *    name="id",
 *    in="path",
 *    type="integer",
 *    description="ID",
 *    required=true
 * ),

返回json語(yǔ)法定義->definitions.php

@SWG\Schema(ref="#/definitions/goods-norm")
文件夾路徑:app/Http/Controller/Document/definitions.php

<?php
/**
 * @SWG\Definition(
 *      definition="201",
 *      required={"message"},
 *      @SWG\Property(
 *          property="message",
 *          type="string",
 *          example="創(chuàng)建成功"
 *      )
 * ),
 ************************************  204  **********************************
 * @SWG\Definition(
 *      definition="204",
 *      required={"message"},
 *      @SWG\Property(
 *          property="message",
 *          type="string",
 *          example="刪除成功"
 *      )
 * ),

Definition @SWG\Schema(ref="#/definitions/json-json"),

  • json下放json
 * @SWG\Definition(
 *     definition="json-json",
 *     required={"json"},
 *     type="object",
 *     allOf={
 *          @SWG\Schema(
 *              @SWG\Property(
 *                  property="json",
 *                  type="object",
 *                  description="json字段",
 *                  required={"name"},
 *                  @SWG\Property(
 *                       property="name",
 *                       type="string",
 *                       example="三腳貓",
 *                       description="用戶名",
 *                  ),
 *              ),
 *          ),
 *     },
 * ),
 ************************************  Demo  **********************************
{
  "json":{
    "name":"三腳貓",
  }
}
  • json下放數(shù)組
 * @SWG\Definition(
 *     definition="json-array",
 *     required={"json"},
 *     type="object",
 *     allOf={
 *          @SWG\Schema(
 *              required={"json"},
 *              @SWG\Property(
 *                  property="json",
 *                  type="array",
 *                  description="json字段",
 *                  required={"name"},
 *                  @SWG\Items(
 *                      @SWG\Property(
 *                          property="name",
 *                          type="string",
 *                          example="三腳貓",
 *                          description="用戶名",
 *                      ),
 *                  ),
 *              ),
 *          ),
 *     },
 * ),
 ************************************  Demo  **********************************
{
  "json":{[
    "name":"三腳貓"
  ],[
    "name":"三腳貓"
  ],}
}

以上都是blog里復(fù)制出來(lái)的,有些文字不必在意,知道是代表的什么意思就好啦。有問(wèn)題留言~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容