安装swagger包
- 链接:swagger包
安装:
composer require zircote/swagger-php
版本:”zircote/swagger-php”: “^4.4”构建Controller和route
```php <?php
namespace App\Http\Controllers\NoMiddle;
use App\Http\Controllers\Controller; use Illuminate\Http\Request; use Swagger\Annotations\Info; use Swagger\Annotations\OA;
/**
- @OA\Info(title=”属于我自己的接口”, version=”0.1”, description=”债务模块的接口以及自己测试的接口”)
*/
class SwaggerController extends Controller
{
public function doc()
{
$swagger = \OpenApi\Generator::scan([
]); return response()->json($swagger); } }app_path('Http/Controllers/NoMiddle'), // 第一个地址里面要有@OA\Info
app_path('Http/Controllers/DebtSelf'), // 其他的控制器目录地址
// swagger 文档 Route::get(‘/swagger/doc’, ‘SwaggerController@doc’);
<a name="ENReT"></a>
# 安装swagger-ui
- 在public下执行`git clone [https://github.com/swagger-api/swagger-ui](https://github.com/swagger-api/swagger-ui)`会将前端代码添加进swagger-ui目录
- git clone拷贝完之后呢,其实我们只需要 dist目录就行,其他的目录以及文件可以删掉
- 将其中的dist中的index.html改为
```html
<!-- HTML for static distribution bundle build -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Swagger UI</title>
<link rel="stylesheet" type="text/css" href="./swagger-ui.css" />
<link rel="stylesheet" type="text/css" href="index.css" />
<link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="./favicon-16x16.png" sizes="16x16" />
</head>
<body>
<div id="swagger-ui"></div>
<script src="./swagger-ui-bundle.js" charset="UTF-8"> </script>
<script src="./swagger-ui-standalone-preset.js" charset="UTF-8"> </script>
<script src="./swagger-initializer.js" charset="UTF-8"> </script>
<script>
window.onload = function() {
// Begin Swagger UI call region
const ui = SwaggerUIBundle({
// url: "https://petstore.swagger.io/v2/swagger.json",
url: "/swagger/doc", //新的配置
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
})
// End Swagger UI call region
window.ui = ui
}
</script>
</body>
</html>
- 就可以直接通过/swagger-ui/dist/index.html路径来访问到swagger文档了
创建新的控制器以及接口
<?php
namespace App\Http\Controllers\DebtSelf;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\ValidaParamService;
/**
* @OA\Tag(
* name="债务",
* description="何日可以出坑",
* )
*/
class DebtIndexController extends Controller
{
private $verifyParamService;
function __construct(
ValidaParamService $verifyParamService
){
$this->verifyParamService = $verifyParamService;
}
/**
* @OA\Post(
* path="/debtSelf/add",
* tags={"债务"},
* summary="添加",
* @OA\Parameter(name="isFastLogin", in="query", @OA\Schema(type="boolean")),
* @OA\Parameter(name="tel", in="query", @OA\Schema(type="string")),
* @OA\Parameter(name="verification_code", in="query", @OA\Schema(type="string")),
* @OA\Parameter(name="password", in="query", @OA\Schema(type="string")),
* @OA\Response(response=200, description=" {err_code: int32, msg:string, data:[]}")
* )
*/
public function add(Request $request)
{
try {
$data = [ "code" => 200, "data" => app_path('Http/Controllers/DebtSelf') ];
$rule = [];
$param = $this->verifyParamService
->init($rule, $request->all())
->filter()
->needRequired()
->param;
return response()->json($data);
} catch(\Exception $e) {
return errorLog($e);
}
}
}
关于注解详细请看:https://github.com/zircote/swagger-php/blob/master/docs/Migrating-to-v3.md