当前位置: 首页 > 后端技术 > PHP

Swagger在laravel语法中表达

时间:2023-03-29 17:00:59 PHP

1.参考链接https://blog.quickadminpanel.com/laravel-api-documentation-with-openapiswagger/https://swagger.io/specification/2.开始在\app\Http\Controllers的Controller.php中添加注释code/***@OA\Info(title="MyFirstAPI",version="0.1")**/classControllerextendsBaseController{useAuthorizesRequests,DispatchesJobs,ValidatesRequests;}在其中添加增删改查路由目录路由Route::resource('swagger',SwaggerController::class);3、CURD操作GET列表查询://listpublicfunctionindex(){/***@OA\Get(*path="/swagger",*tags={"Swagger增删改查"},*description="检查列表",*summary="检查列表",*@OA\Response(response="200",description="示例资源")*)*/$arr=['egg'=>'adf','php'=>'qqq'];返回json_encode($arr);}GETquery//显示对应id的内容publicfunctionshow($id){/***@OA\Get(*path="/swagger/{id}",*tags={"Swagger添加,删除,修改,检查"},*summary="Query",*description="根据id获取数据",*@OA\Parameter(in="path",name="id",description="typeid",required=true,@OA\Schema(type="integer"),),*@OA\Response(response="200",description="示例资源"),**)*/$arr=[1=>['php','asp'],2=>['aaa','测试']];返回json_encode($arr[$id]);}post增加(这里以多文件上传为例)//添加publicfunctionstore(Request$request){/***@OA\Post(*path="/swagger",*tags={"SwaggerCRUD"},*summary="Add",*description="Adddata",*@OA\Parameter(in="query",name="username",description="username",required=true,@OA\Schema(type="string"),),*@OA\Parameter(in="query",name="age",description="age",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="要上传的文件",*property="文件[]",*type="array",*@OA\Items(type="file"),*),**)*)*),*@OA\Response(response="200",description="示例资源"),**)*/$input=$request->all();$input['file']=$_FILES['file'];returnjson_encode($input);}PUT更新(单文件上传)//更新publicfunctionupdate(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="整数",*),*@OA\Property(*description="name",*property="name",*type="string",*),*@OA\Property(*description="年龄”,*property="年龄",*type="整数",*),**)*)*),*@OA\Response(response="200",description="示例资源"),**)*/$input=$request->all();返回json_encode($input);}删除//删除publicfunctiondestroy(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="示例资源"),**)*/$input=$request->all();返回json_encode($input);}