Swagger在laravel 语法表达

一、参考链接
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);
    }

Swagger在laravel 语法表达_第1张图片

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]);

    }

Swagger在laravel 语法表达_第2张图片

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);
     
     
    }

Swagger在laravel 语法表达_第3张图片

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);
    }

Swagger在laravel 语法表达_第4张图片

删除

//删除
    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);
    } 

Swagger在laravel 语法表达_第5张图片

你可能感兴趣的:(php)