PHP code example of cblink / yapi-doc
1. Go to this page and download the library: Download cblink/yapi-doc library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
cblink / yapi-doc example snippets
return [
// yap请求地址
'base_url' => 'http://yapi.cblink.net/',
// 文档合并方式,"normal"(普通模式) , "good"(智能合并), "merge"(完全覆盖)
'merge' => 'merge',
// token配置
'config' => [
// key名称可以自定义,默认是 default ,可以增加多个token配置
'default' => [
// yapi project id
'id' => 1,
// yapi project token
'token' => 'xxxxxx'
],
],
'public' => [
// 返回值前缀
'prefix' => 'data',
// 公共的请求参数,query部分
'query' => [
// 格式 key => [] , key为返回值名称
// plan 的返回参数说明,
// must,无论接口返回是否返回,都出现在yapi文档中
// 'must' => false,
'children' => [
'current_page' => ['plan' => '当前页数'],
'total' => ['plan' => '总数量'],
'per_page' => ['plan' => '每页数量'],
]
]
]
];
use Tests\TestCase;
class BaseTestCase extends TestCase
{
public function test_store_products()
{
// 接口请求
$res = $this->post('/api/product', [
'product_name' => '商品名称',
'price' => 500
]);
// 参数 request, Illuminate\Http\Request
$request = $this->app['request'];
// 参数 response,Illuminate\Http\Response
$response = $res->baseResponse;
// 参数 dto
$dto = new \Cblink\YApiDoc\YapiDTO([
// 这里填多项会同时发布到多个项目组中
'project' => ['default', 'custom'],
'name' => '接口名称',
'category' => '接口分组',
'request' => [
'trans' => [
''
],
'expect' => [
],
],
'response' => [
'trans' => [
// 数组的说明,如果是需要给数组加说明,需要带上*
// 多层级可以嵌套, *.products.*.name => '订单商品名称'
'*.name' => '',
'price' => ''
],
// 允许非必填的字段
'expect' => [
'*' => ['name', 'price'],
'price',
'aaa' => ['name']
],
]
]);
$yapi = new \Cblink\YApiDoc\YApi($request, $response, $dto);
// 生成文件
$yapi->make();
}
}
shell script
php artisan vendor:publish --provider=Cblink\YApiDoc\ServiceProvider::class
shell script
php artisan upload:yapi