這篇“Laravel Swagger怎么使用”文章的知識點大部分人都不太理解,所以小編給大家總結了以下內容,內容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“Laravel Swagger怎么使用”文章吧。
本教程是基于Laravel 生成swagger 為例子,其實這個東西和語言或者和框架基本沒啥區別,因為都是用的公用的json ,通過程序掃描swagger預先規定的“語言”,生成結構存入json中,通過 swagger ui 展現出來(或者自己開發)。
對于php開發人員來說,有大部分同學很不喜歡swagger。 因為這個看上去寫起來好麻煩啊,一想到分分鐘用php寫完的代碼,寫swagger要寫10分鐘,心里就抵觸這個東西。
身邊有Java開發的同學就知道他們很大一部分都用swagger,因為java要維護數據結構,而且swagger在java整合得更靈活。
這個時候java如果看到有php 說swagger反人類的東西,太麻煩了,上古時代的產物。那身邊的java朋友會心里竊喜,這么好用的東西都不用,還說php是世界上最好的語言。
最近在寫自動生成代碼,其實現在Laravel 很多自動生成CURD的。比如像laravel-admin ,一條命令生成CURD,但是生成之后,數據看上去很冷。 比如有一些字段不需要顯示,有一些是要select關聯枚舉的,有一些是 hasMany的,還有 overtrue(正超)的api腳手架也挺好的
所以swaager也可以根據業務需求寫自動化生成
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" * ) * ) */
如果要匹配path中的數值則 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"}
* }
* },
* )
*/
/**
* @OA\Post(
* path="/api/test/store",
* operationId="api/test/store",
* tags={"Test"},
* summary="Test創建",
* description="Test提交創建",
* @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":{}
* }
* },
* )
*/* @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="狀態",
* required=true,
* explode=true,
* @OA\Schema(
* type="array",
* default="available",
* @OA\Items(
* type="string",
* enum = {"available", "pending", "sold"},
* )
* )
* ), * @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"}
* )
* )
* ),* @OA\RequestBody( * description="order placed for purchasing th pet", * required=true, * @OA\JsonContent(ref="#/components/schemas/UserModel") * ),
/**
* @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,
* )
* )
*/一個枚舉單獨創建一個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" * ),
這樣前端開發者就可以
和枚舉差不多,通過一個Property關聯模型
* @OA\Property( * property="user_detail", * ref="#/components/schemas/UserModel2" * ),
關聯模型和枚舉,可以自動生成請求的參數和,返回的結構
* @OA\Response( * response=200, * description="successful operation", * @OA\JsonContent( * type="array", * @OA\Items(ref="#/components/schemas/UserModel"), * @OA\Items(ref="#/components/schemas/UserModel2") * ) * ),
就比如那天前端小妹跟你說,哥哥,支付狀態3代表什么,可能你很快的說出了是某某狀態,但是問你11是啥狀態,人都要沙雕了。
通過swagger 的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,
* ),
* )
* }
* )
*//** * @OA\SecurityScheme( * type="apiKey", * in="query", * securityScheme="api_key", * name="api_key" * ) */
在接口中添加
security={{"api_key": {}}},這時,swagger Ui 會出現一個鎖一樣的東西
可以輸入自己的token,請求的時候會帶上token
線上環境如果訪問不了,可能是你nginx 配置的問題,因為,laravel-swagger 是通過file_content_get() 的方式 echo 輸出js 的。而你的nginx 配置判斷,如果是 .js 或者css 是靜態文件,所以到不了index.php ,更執行不了 file_content_get 函數了??梢詤⒖糿ginx 配置:
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怎么使用”這篇文章的內容,相信大家都有了一定的了解,希望小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。