api文档使用swagger

注:版本要求laravel5.6 参考链接

1、安装l5-swagger

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

2、发布

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

3、安装swagger-php

composer require 'zircote/swagger-php:2.*'

4、在.env中将环境变量设置SWAGGER_VERSION为2.0:

SWAGGER_VERSION=2.0

5、在app/Http/Controllers/Controller.php文件中class前添加注释

7、生成控制器

php artisan make:controller TestSwaggerController
class TestSwaggerController 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' => '[email protected]'
                    ]
                ],
            ],
            'message'   => '',
            'type'      => 'success',
            'status'    => 0
        ]);
    }
}

6、生成api文档后访问 http://localhost:8000/api/documentation

php artisan l5-swagger:generate

7、部署到centos lnmp 服务器出现 css js 404情况解决方案,注释掉重启服务器

#    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
#    {
#        expires      30d;
#    }

#    location ~ .*\.(js|css)?$
#    {
#        expires      12h;
#    }

 

你可能感兴趣的:(萌芽科技后台开发规范)