Laravel5.6中集成L5 Swagger接口文档工具

官方项目地址:https://github.com/DarkaOnLine/L5-Swagger

引入项目

composer require "darkaonline/l5-swagger:5.6.*"

生成配置及初始化文件

php artisan vendor:publish --provider "L5Swagger\L5SwaggerServiceProvider"

添加API相关描述信息及Demo

在app目录或Controller目录下创建文件:
文件及内容:app/Swagger.php

生成api-docs.json文件

php artisan l5-swagger:generate

访问测试

http://127.0.0.1/api/documentation

在测试控制器中添加

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TestController extends Controller
{
    /**
     * @SWG\Get(
     *     path="/api/test",
     *     description="返回测试内容",
     *     operationId="api.dashboard.index",
     *     produces={"application/json"},
     *     tags={"测试"},
     *     @SWG\Parameter(
     *         in="formData",
     *         name="reason",
     *         type="string",
     *         description="拿数据的理由",
     *         required=true,
     *     ),
     *     @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
        ]);
    }
}

更新文档内容

php artisan l5-swagger:generate

效果

QQ截图20180821145326.png

标签: 无

发表评论: