Open JesseyChen opened 5 years ago
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ExampleController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
//
}
public function store(Request $request)
{
}
public function index(Request $request)
{
}
public function show($id)
{
}
public function update(Request $request, $id)
{
}
public function destroy($id)
{
}
/**
*
* @OA\Get(
* path="/examples",
* operationId="/sample/category/things",
* tags={"example"},
* summary="get demo",
* @OA\Parameter(
* name="id",
* in="path",
* description="id",
* required=true,
* @OA\Schema(type="string")
* ),
* @OA\Parameter(
* name="auth",
* in="header",
* description="auth",
* required=false,
* @OA\Schema(type="string")
* ),
* @OA\Response(
* response="200",
* description="Returns some sample category things",
* @OA\JsonContent()
* ),
* @OA\Response(
* response="400",
* description="Error: Bad request. When required parameters were not supplied.",
* ),
* security={ {"bearer": {}} },
* )
*
* @OA\Get(
* path="/examples/{id}",
* operationId="/sample/category/things",
* tags={"example"},
* summary="get demo",
* @OA\Parameter(
* name="id",
* in="path",
* description="id",
* required=true,
* @OA\Schema(type="string")
* ),
* @OA\Parameter(
* name="auth",
* in="header",
* description="auth",
* required=false,
* @OA\Schema(type="string")
* ),
* @OA\Response(
* response="200",
* description="Returns some sample category things",
* @OA\JsonContent()
* ),
* @OA\Response(
* response="400",
* description="Error: Bad request. When required parameters were not supplied.",
* ),
* security={ {"bearer": {}} },
*
* )
*
* @OA\Post(
* path="/examples",
* summary="post demo",
* tags={"example"},
* @OA\Parameter(
* name="Authorization",
* in="header",
* description="Authorization Token",
* required=true,
* @OA\Schema(type="string")
* ),
* @OA\RequestBody(
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(
* @OA\Property(
* property="id",
* type="string"
* ),
* @OA\Property(
* property="name",
* type="string"
* ),
* example={"id": 10, "name": "Jessica Smith"}
* )
* )
* ),
*
* @OA\Response(
* response="200",
* description="GET",
*
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(
* example={"errcode":0 , "msg": "ok", "data": {"id":10,"name":"jack"} }
* )
* )
*
* ),
* @OA\Response(
* response="201",
* description="PUT/POST/PATCH成功",
* ),
* @OA\Response(
* response="401",
* description="权限不够",
* ),
* @OA\Response(
* response="403",
* description="禁止访问",
* ),
* @OA\Response(
* response="404",
* description="找不到地址",
* ),
* )
* @OA\Delete(
* path="/examples/{id}",
* summary="delete demo",
* tags={"example"},
* @OA\Parameter(
* name="id",
* in="path",
* description="The category parameter in path",
* required=true,
* @OA\Schema(type="string")
* ),
*
* @OA\Parameter(
* name="Authorization",
* in="header",
* description="Authorization Token",
* required=true,
* @OA\Schema(type="string")
* ),
* @OA\RequestBody(
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(
* @OA\Property(
* property="id",
* type="string"
* ),
* @OA\Property(
* property="name",
* type="string"
* ),
* example={"id": 10, "name": "Jessica Smith"}
* )
* )
* ),
*
* @OA\Response(
* response="200",
* description="GET",
*
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(
* example={"errcode":0 , "msg": "ok", "data": {"id":10,"name":"jack"} }
* )
* )
*
* ),
* @OA\Response(
* response="201",
* description="PUT/POST/PATCH成功",
* ),
* @OA\Response(
* response="401",
* description="权限不够",
* ),
* @OA\Response(
* response="403",
* description="禁止访问",
* ),
* @OA\Response(
* response="404",
* description="找不到地址",
* ),
* )
* @OA\Put(
* path="/examples",
* summary="put demo",
* tags={"example"},
* @OA\Parameter(
* name="Authorization",
* in="header",
* description="Authorization Token",
* required=true,
* @OA\Schema(type="string")
* ),
* @OA\RequestBody(
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(
* @OA\Property(
* property="id",
* type="string"
* ),
* @OA\Property(
* property="name",
* type="string"
* ),
* example={"id": 10, "name": "Jessica Smith"}
* )
* )
* ),
*
* @OA\Response(
* response="200",
* description="GET",
*
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(
* example={"errcode":0 , "msg": "ok", "data": {"id":10,"name":"jack"} }
* )
* )
*
* ),
* @OA\Response(
* response="201",
* description="PUT/POST/PATCH成功",
* ),
* @OA\Response(
* response="401",
* description="权限不够",
* ),
* @OA\Response(
* response="403",
* description="禁止访问",
* ),
* @OA\Response(
* response="404",
* description="找不到地址",
* ),
* )
* */
}
Step1. 初始化Lumen项目
composer create-project laravel/lumen lumen_swagger
Step2. 找一找Packagist里有没有支持lumen下的拓展包
Step3. 使用composer安装拓展包
composer require "darkaonline/swagger-lume:5.6.*"
step4. 做一些准备工作, 在 bootstrap/app.php
去掉注释
$app->withFacades();
加载swagger配置,singleton语句后面添加
$app->configure('swagger-lume');
注册服务 SwaggerLume\ServiceProvider
$app->register(\SwaggerLume\ServiceProvider::class);
放comment了…
step5. 将扩展包的的配置文件
php artisan swagger-lume:publish
到这里准备工作就完成了. 现在说一下如何使用.
Step6. 创建SwaggerController.php控制器,把头部信息写进去
Stept7. 创建ExampleController.php控制器
.. 放comment里
然后执行
php artisan swagger-lume:generate
在这里会生成一个json文件,视图会打印这个文件的内容
Step8. 路由写成restful风格
![Uploading image.png…]()
最后效果图,可以一边写文档一边测试,把测试数据写进swagger里
地址:http://xxx.com/api/documentation