api文档使用swagger

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Bepthslowly/article/details/87856170

注:版本要求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前添加注释

<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
/**
 * @SWG\Swagger(
 *   basePath="/calculate-rates",
 *   @SWG\Info(
 *     title="项目名称 API",
 *     version="1.0.0"
 *   )
 * )
 */
class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}

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

猜你喜欢

转载自blog.csdn.net/Bepthslowly/article/details/87856170