swagger ui php,tp5 swagger-ui文档生成

如何生成swagger-ui需要的json或者yaml的文件?

tp5根目录, composer require zircote/swagger-php

方案1: 有需要的时候手动执行命令生成api_doc.json文件

添加命令

application/command.php 新增一行: \app\common\command\run\AutoCreateApiDoc::class, 就可以执行 php think run:autoCreateApiDoc 手动生成swagger json配置文件, 配置文件所在路径 public/api_doc.json

/**

* Created by querying.

* Date: 19-2-20

* Time: 下午5:15

*/

namespace app\common\command\run;

use function \OpenApi\scan;

use think\console\Command;

use think\console\Input;

use think\console\Output;

use think\facade\App;

class AutoCreateApiDoc extends Command

{

protected function configure()

{

$this->setName("run:autoCreateApiDoc")->setDescribe("生成swagger-api文档");

}

protected function execute(Input $input, Output $output)

{

try {

$openapi = scan(App::getAppPath());

header('Content-Type: application/x-yaml');

$jsonStr = $openapi->toJson();

file_put_contents(App::getRootPath(). '/public/api_doc.json', $jsonStr);

} catch (\Throwable $e) {

echo $e->__toString();

}

}

}

方案2: swagger-ui直接输入的url,实时返回配置文件信息(优点:接口文档实时更新,缺点:每次打开时间较长)

新增接口

$openapi = scan(App::getAppPath());

header('Content-Type: application/x-yaml');

$jsonStr = $openapi->toJson();

echo $jsonStr;

部署swagger-ui,自动化测试接口文档

下载现成的ui组件

git clone https://github.com/swagger-api/swagger-ui.git

修改swagger.json文件地址,路径public/swagger-ui/dist/index.html

修改对应的url就可以了

window.onload = function() {

const ui = SwaggerUIBundle({

// 改动这里 ===========

// 改动这里, 这就是通过http请求,获取到swagger-json数据

// 这里更换下url就好了

url: "http://engine/api_doc.json",

dom_id: '#swagger-ui',

deepLinking: true,

presets: [

SwaggerUIBundle.presets.apis,

SwaggerUIStandalonePreset

],

plugins: [

SwaggerUIBundle.plugins.DownloadUrl

],

layout: "StandaloneLayout"

})

window.ui = ui

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值