thinkphp6.0 集成 Swagger
- 下载swagger-ui
git clone https://github.com/swagger-api/swagger-ui.git
- 复制swagger-ui目录下dist文件到php项目根目录 eg:public
cp -r dist rootpath/public
- 打开php项目安装swagger-php拓展
composer require zircote/swagger-php
- 添加接口路由
Route::get('/swagger', function (){
$openapi = \OpenApi\Generator::scan(['../app/adminapi/']);
echo $openapi->toYaml();
});
- 编辑dist目录下swagger-initializer.js 文件,修改url地址为文档路由地址
window.onload = function() {
//<editor-fold desc="Changeable Configuration Block">
// the following lines will be replaced by docker/configurator, when it runs in a docker-container
window.ui = SwaggerUIBundle({
// url: "http://wgp.com/adminapi/swagger",
urls:[
{url:"http://wgp.com/adminapi/swagger",name:"后台管理接口文档"},
{url:"http://wgp.com/api/swagger",name:"前端小程序接口文档"}
],
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
});
//</editor-fold>
};
鉴权
/**
*@OA\SecurityScheme(type="http",securityScheme="Authorization",name="Authorization",in="header",scheme="Bearer",bearerFormat="JWT")
*/
类库 https://github.com/zircote/swagger-php 参考 https://blog.csdn.net/urmljyc/article/details/103422830 参考 https://blog.csdn.net/qq_22146915/article/details/124068193