Laravel (Post, Delete, Put) routes in Swagger

2019-07-19 05:21发布

问题:

I have update the code for get method as follows which working properly in swagger.

Can any one please suggest me the swagger code for post, put, delete with its laravel route, controller code. (As I mention follows for GET)

route/web.php

        Route::group(['prefix' => 'api/'], function () {
           Route::get('dashboard', 'DashboardController@index');
        });

DashboardController.php

 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\JsonResponse
 *
 * @SWG\Get(
 *     path="/api/dashboard",
 *     description="Returns dashboard overview.",
 *     operationId="api.dashboard.index",
 *     produces={"application/json"},
 *     tags={"dashboard"},
 *     @SWG\Response(
 *         response=200,
 *         description="Dashboard overview."
 *     ),
 *     @SWG\Response(
 *         response=401,
 *         description="Unauthorized action.",
 *     )
 * )
 */

public function index(Request $request)
{
    return response()->json([
        'result'    => [
            'statistics' => [
                'users' => [
                    'name'  => 'Name',
                    'email' => 'user@example.com'
                ]
            ],
        ],
        'message'   => '',
        'type'      => 'success',
        'status'    => 0
    ]);
}  

回答1:

Bellow laravel routes for swagger Post/Put/Delete.

Route::post('/api/user', 'DashboardController@store');
Route::put('/api/user/{user_id}', 'DashboardController@edit');
Route::delete('/api/user/{user_id}', 'DashboardController@delete');
Route::get('/api/users', 'DashboardController@getData');
Route::get('/api/user/{user_id}', 'DashboardController@getDataById');

For Post

   /**
     * @SWG\Post(
     *      path="/api/user",
     *      tags={"User"},
     *      operationId="ApiV1saveUser",
     *      summary="Add User",
     *      consumes={"application/x-www-form-urlencoded"},
     *      produces={"application/json"},
     *      @SWG\Parameter(
     *          name="name",
     *          in="formData",
     *          required=true, 
     *          type="string" 
     *      ),
     *      @SWG\Parameter(
     *          name="phone",
     *          in="formData",
     *          required=true, 
     *          type="number" 
     *      ),
     *      @SWG\Response(
     *          response=200,
     *          description="Success"
     *      ),
     */

For Put

   /**
     * @SWG\Put(
     *      path="/api/user/{user_id}",
     *      tags={"User"},
     *      operationId="ApiV1UpdateUser",
     *      summary="Update User",
     *      consumes={"application/x-www-form-urlencoded"},
     *      produces={"application/json"},
     *      @SWG\Parameter(
     *          name="user_id",
     *          in="path",
     *          required=true, 
     *          type="string" 
     *      ),
     *      @SWG\Parameter(
     *          name="name",
     *          in="formData",
     *          required=true, 
     *          type="string" 
     *      ),
     *      @SWG\Response(
     *          response=200,
     *          description="Success"
     *      ),
     */

For Delete By ID

   /**
     * @SWG\Delete(
     *      path="/api/users",
     *      tags={"User"},
     *      operationId="ApiV1DeleteUser",
     *      summary="Delete User",
     *      @SWG\Parameter(
     *          name="user_id",
     *          in="path",
     *          required=true, 
     *          type="string" 
     *      ),
     *      @SWG\Response(
     *          response=200,
     *          description="Success"
     *      ),
     */

For Get

    /**
     * @SWG\Get(
     *      path="/api/users",
     *      tags={"User"},
     *      operationId="ApiV1GetUsers"
     *      summary="Get Users",
     *      @SWG\Response(
     *          response=200,
     *          description="Success"
     *      ),
     */

For Get By ID

    /**
     * @SWG\Get(
     *      path="/api/user/{user_id}",
     *      tags={"User"},
     *      operationId="ApiV1GetUserById",
     *      summary="Get User by user id",
     *      @SWG\Parameter(
     *          name="user_id",
     *          in="path",
     *          required=true, 
     *          type="string" 
     *      ),
     *      @SWG\Response(
     *          response=200,
     *          description="Success"
     *      ),
     */


回答2:

I have found code parameters for post, delete routes at following link

https://github.com/zircote/swagger-php/tree/master/Examples