溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Laravel Swagger怎么使用

發(fā)布時間:2022-04-12 10:41:52 來源:億速云 閱讀:210 作者:iii 欄目:編程語言

這篇“Laravel Swagger怎么使用”文章的知識點大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“Laravel Swagger怎么使用”文章吧。

swagger太辣雞了?

本教程是基于Laravel 生成swagger 為例子,其實這個東西和語言或者和框架基本沒啥區(qū)別,因為都是用的公用的json ,通過程序掃描swagger預先規(guī)定的“語言”,生成結(jié)構(gòu)存入json中,通過 swagger ui 展現(xiàn)出來(或者自己開發(fā))。

對于php開發(fā)人員來說,有大部分同學很不喜歡swagger。 因為這個看上去寫起來好麻煩啊,一想到分分鐘用php寫完的代碼,寫swagger要寫10分鐘,心里就抵觸這個東西。

身邊有Java開發(fā)的同學就知道他們很大一部分都用swagger,因為java要維護數(shù)據(jù)結(jié)構(gòu),而且swagger在java整合得更靈活。

這個時候java如果看到有php 說swagger反人類的東西,太麻煩了,上古時代的產(chǎn)物。那身邊的java朋友會心里竊喜,這么好用的東西都不用,還說php是世界上最好的語言。

我為啥用swagger

最近在寫自動生成代碼,其實現(xiàn)在Laravel 很多自動生成CURD的。比如像laravel-admin ,一條命令生成CURD,但是生成之后,數(shù)據(jù)看上去很冷。 比如有一些字段不需要顯示,有一些是要select關聯(lián)枚舉的,有一些是 hasMany的,還有 overtrue(正超)的api腳手架也挺好的

所以swaager也可以根據(jù)業(yè)務需求寫自動化生成

L5-Swagger

安裝:

composer require "darkaonline/l5-swagger"

使用:

php artisan vendor:publish --provider "L5Swagger\L5SwaggerServiceProvider"
php artisan l5-swagger:generate

填寫下面例子生成之后再訪問

/api/documentation

@OA\Info 為必須

例子

/**
 * @OA\Info(
 *      version="1.0.0",
 *      title="L5 OpenApi",
 *      description="L5 Swagger OpenApi description",
 *      @OA\Contact(
 *          email="darius@matulionis.lt"
 *      ),
 *     @OA\License(
 *         name="Apache 2.0",
 *         url="http://www.apache.org/licenses/LICENSE-2.0.html"
 *     )
 * )
 */
get 請求

如果要匹配path中的數(shù)值則 in path 查詢 in query

/**
 * @OA\Get(
 *      path="/projects/{id}",
 *      operationId="getProjectById",
 *      tags={"Projects"},
 *      summary="Get project information",
 *      description="Returns project data",
 *      @OA\Parameter(
 *          name="id",
 *          description="Project id",
 *          required=true,
 *          in="path",
 *          @OA\Schema(
 *              type="integer"
 *          )
 *      ),
 *      @OA\Response(
 *          response=200,
 *          description="successful operation"
 *       ),
 *      @OA\Response(response=400, description="Bad request"),
 *      @OA\Response(response=404, description="Resource Not Found"),
 *      security={
 *         {
 *             "oauth3_security_example": {"write:projects", "read:projects"}
 *         }
 *     },
 * )
 */
POST 請求
              
    /**
     * @OA\Post(
     *      path="/api/test/store",
     *      operationId="api/test/store",
     *      tags={"Test"},
     *      summary="Test創(chuàng)建",
     *      description="Test提交創(chuàng)建",
     *      @OA\Parameter(
     *          name="id",
     *          description="",
     *          required=false,
     *          in="query",
     *      ),
     *     @OA\Response(
     *         response=200,
     *         description="successful operation",
     *         @OA\JsonContent(
     *         ref="#/components/schemas/Test"
     *         )
     *     ),
     *      @OA\Response(response=400, description="Bad request"),
     *      @OA\Response(response=404, description="Resource Not Found"),
     *      security={
     *         {
     *             "api_key":{}
     *         }
     *     },
     * )
     */

文件上傳參數(shù)

     *     @OA\RequestBody(
     *       @OA\MediaType(
     *           mediaType="multipart/form-data",
     *           @OA\Schema(
     *               type="object",
     *               @OA\Property(
     *                  property="file",
     *                  type="file",
     *               ),
     *           ),
     *       )
     *     ),

傳入為枚舉

     *     @OA\Parameter(
     *         name="status",
     *         in="query",
     *         description="狀態(tài)",
     *         required=true,
     *         explode=true,
     *         @OA\Schema(
     *             type="array",
     *             default="available",
     *             @OA\Items(
     *                 type="string",
     *                 enum = {"available", "pending", "sold"},
     *             )
     *         )
     *     ),

Body 為Json 方式提交

     *     @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 @OA\Property(
     *                     property="id",
     *                     type="string"
     *                 ),
     *                 @OA\Property(
     *                     property="name",
     *                     type="string"
     *                 ),
     *                 example={"id": 10, "name": "Jessica Smith"}
     *             )
     *         )
     *     ),
使用結(jié)構(gòu)Schema作為請求參數(shù)
     *     @OA\RequestBody(
     *         description="order placed for purchasing th pet",
     *         required=true,
     *         @OA\JsonContent(ref="#/components/schemas/UserModel")
     *     ),
Schema的使用
/**
 * @OA\Schema(
 *      schema="UserModel",
 *      required={"username", "age"},
 *      @OA\Property(
 *          property="username",
 *          format="string",
 *          description="用戶名稱",
 *          example="小廖",
 *      ),
 *      @OA\Property(
 *          property="age",
 *          format="int",
 *          description="年齡",
 *          example=1,
 *          nullable=true,
 *      )
 * )
 */

枚舉

一個枚舉單獨創(chuàng)建一個Schema

/**
 * @OA\Schema(
 *   schema="product_status",
 *   type="string",
 *   description="The status of a product",
 *   enum={"available", "discontinued"},
 *   default="available"
 * )
 */

映射到模型中的具體字段

 *      @OA\Property(
 *     property="status",
 *     ref="#/components/schemas/product_status"
 *      ),

這樣前端開發(fā)者就可以

關聯(lián)模型

和枚舉差不多,通過一個Property關聯(lián)模型

 *      @OA\Property(
 *     property="user_detail",
 *     ref="#/components/schemas/UserModel2"
 *      ),

關聯(lián)模型和枚舉,可以自動生成請求的參數(shù)和,返回的結(jié)構(gòu)

返回為模型結(jié)構(gòu)

     *     @OA\Response(
     *         response=200,
     *         description="successful operation",
     *         @OA\JsonContent(
     *             type="array",
     *             @OA\Items(ref="#/components/schemas/UserModel"),
     *             @OA\Items(ref="#/components/schemas/UserModel2")
     *         )
     *     ),

就比如那天前端小妹跟你說,哥哥,支付狀態(tài)3代表什么,可能你很快的說出了是某某狀態(tài),但是問你11是啥狀態(tài),人都要沙雕了。
通過swagger 的Schema 能讓前端人員摸清后端的結(jié)構(gòu)信息

多個合并Schema

/**
 * @OA\Schema(
 *   schema="UserModel",
 *   allOf={
 *     @OA\Schema(ref="#/components/schemas/UserModel2"),
 *     @OA\Schema(
 *       type="object",
 *       description="Represents an authenticated user",
 *       required={
 *         "email",
 *         "role",
 *       },
 *       additionalProperties=false,
 *       @OA\Property(
 *         property="email",
 *         type="string",
 *         example="user@example.com",
 *         nullable=true,
 *       ),
 *     )
 *   }
 * )
 */

驗證提供outh3 和apikey 兩種方式,在存放全局配置中寫入(也可以任意目錄中)

/**
 * @OA\SecurityScheme(
 *     type="apiKey",
 *     in="query",
 *     securityScheme="api_key",
 *     name="api_key"
 * )
 */

在接口中添加

security={{"api_key": {}}},

這時,swagger Ui 會出現(xiàn)一個鎖一樣的東西

可以輸入自己的token,請求的時候會帶上token

可能遇到的問題

線上環(huán)境如果訪問不了,可能是你nginx 配置的問題,因為,laravel-swagger 是通過file_content_get() 的方式 echo 輸出js 的。而你的nginx 配置判斷,如果是 .js 或者css 是靜態(tài)文件,所以到不了index.php ,更執(zhí)行不了 file_content_get 函數(shù)了。可以參考nginx 配置:

charset utf-8;
client_max_body_size 128M;

location / {
	try_files $uri $uri/ /index.php$is_args$args;
}


location ~ \.php$ {
	include fastcgi_params;
	fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
	fastcgi_pass  php74:9000 這個換成你自己的;
	try_files $uri =404;
}

以上就是關于“Laravel Swagger怎么使用”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關的知識內(nèi)容,請關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI