一、参考链接
https://blog.quickadminpanel.com/laravel-api-documentation-with-openapiswagger/
https://swagger.io/specification/
二、开始
在 \app\Http\Controllers 的 Controller.php 加上一下正文代码
/**
* @OA\Info(title="My First API", version="0.1")
*
*/
class Controller extends BaseController
{use AuthorizesRequests, DispatchesJobs, ValidatesRequests;}
在 routes 目录外面增加增删改查路由
Route::resource('swagger',SwaggerController::class);
三、CURD 操作
GET 列表查问:
// 列表
public function index(){
/**
* @OA\Get(
* path="/swagger",
* tags={"Swagger 增删改查"},
* description="查列表",
* summary="查列表",
* @OA\Response(response="200", description="An example resource")
* )
*/
$arr = ['egg'=>'adf','php'=>'qqq'];
return json_encode($arr);
}
GET 查问
// 显示对应 id 的内容
public function show($id){
/**
* @OA\Get(* path="/swagger/{id}",
* tags={"Swagger 增删改查"},
* summary="查问",
* description="依据 id 获取数据",
* @OA\Parameter(in="path",name="id",description="类型 id",required=true,@OA\Schema(type="integer"),),
* @OA\Response(response="200", description="An example resource"),
*
* )
*/
$arr = [1=>['php','asp'],
2=>['aaa','test']
];
return json_encode($arr[$id]);
}
post 减少(这里是多文件上传示例)
// 减少
public function store(Request $request){
/**
* @OA\Post(
* path="/swagger",
* tags={"Swagger 增删改查"},
* summary="减少",
* description="减少数据",
* @OA\Parameter(in="query",name="username",description="用户名称",required=true,@OA\Schema(type="string"),),
* @OA\Parameter(in="query",name="age",description="年龄",required=true,@OA\Schema(type="integer"),),
* @OA\Parameter(
* in="query",
* name="key[]",
* @OA\Schema(* type="array", collectionFormat="multi", @OA\Items(type="string"),uniqueItems=true,
* )
* ),
* @OA\RequestBody(
* required=true,
* @OA\MediaType(
* mediaType="multipart/form-data",
* @OA\Schema(
* @OA\Property(
* description="file to upload",
* property="file[]",
* type="array",
* @OA\Items(type="file"),
* ),
*
* )
* )
* ),
* @OA\Response(response="200", description="An example resource"),
*
* )
*/
$input=$request->all();
$input['file'] = $_FILES['file'];
return json_encode($input);
}
PUT 更新(单文件上传)
// 更新
public function update(Request $request){
/**
* @OA\Put(* path="/swagger/{id}",
* tags={"Swagger 增删改查"},
* summary="更新",
* description="更新数据",
* @OA\RequestBody(
* required=true,
* @OA\MediaType(
* mediaType="application/x-www-form-urlencoded",
* @OA\Schema(
* @OA\Property(
* description="id",
* property="id",
* type="integer",
* ),
* @OA\Property(
* description="名称",
* property="name",
* type="string",
* ),
* @OA\Property(
* description="年纪",
* property="age",
* type="integer",
* ),
*
* )
* )
* ),
* @OA\Response(response="200", description="An example resource"),
*
* )
*/
$input=$request->all();
return json_encode($input);
}
删除
// 删除
public function destroy(Request $request){
/**
* @OA\Delete(* path="/swagger/{id}",
* tags={"Swagger 增删改查"},
* summary="删除",
* description="删除数据",
* @OA\RequestBody(
* required=true,
* @OA\MediaType(
* mediaType="application/x-www-form-urlencoded",
* @OA\Schema(
* @OA\Property(
* description="id",
* property="id",
* type="integer",
* ),
*
* )
* )
* ),
* @OA\Response(response="200", description="An example resource"),
*
* )
*/
$input=$request->all();
return json_encode($input);
}