PHP code example of busyphp / api-doc
1. Go to this page and download the library: Download busyphp/api-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/ */
busyphp / api-doc example snippets
/**
* 这里写接口分组名称
* @desc 分组说明,说明这个分组的作用
*/
class TestController extends \BusyPHP\Controller {
}
class TestController extends \BusyPHP\Controller {
// 以下是书写格式
/**
* 这里写接口说明,如:获取用户资料
* @api post
* @param int *test 参数1说明
* @param string test2 参数2说明 [
* # type_a: 用户ID
* # type_b: 用户姓名
* ]
* @param string test3 参数3....
* @return Json [
* # list array: 消息集合 [
* ## id string: 消息ID
* ## title string: 消息标题
* ## read boolean: 是否已读
* ## operate object: 操作结构 [
* ### type int: 操作类型,`1 打开页面`,`2 打开内部浏览器`,`3 打开外部浏览器`
* ### value string: 操作内容
* ]
* ]
* ]
* @example {list:[], page: 1}
* @desc 说明这个接口的功能等
*/
public function test() {
}
}
class ShowController extends \BusyPHP\Controller {
/**
* 展示接口文档
*/
public function doc() {
$apiDoc = new \BusyPHP\apidoc\ApiDoc('要解析的类文件路径', '要解析的类名等');
$apiDoc->getScan()->removeSuffix('移除控制器类名指定的后缀', '移除方法名指定的后缀');
$apiDoc->getScan()->removeSuffix(function($name, $class) {
// 闭包处理控制器类名称
return '处理后的类名称';
}, function($name, $class) {
// 闭包处理方法名称
return '处理后的方法名称';
});
$apiDoc->getInfo()->setTitle('文档标题');
$apiDoc->getInfo()->setDebugRootUrl('测试模式入口地址');
$apiDoc->getInfo()->setReleaseRootUrl('发布模式入口地址');
$apiDoc->getInfo()->setDescription('文档说明');
// 添加错误代码
$apiDoc->getInfo()->addErrorCode('错误代码', '说明', '解决方法');
// 全局请求头参数
$apiDoc->getInfo()->addHeader('header_params', true, '请求头参数说明');
// 添加全局返回结构
$apiDoc->getInfo()->addReturn(new \BusyPHP\apidoc\structs\ReturnItem('参数', '类型', new \BusyPHP\apidoc\structs\DataStructure('参数说明')));
// 添加全局参数
$apiDoc->getInfo()->addParam(new \BusyPHP\apidoc\structs\ParamItem('param1', 'boolean', true, new \BusyPHP\apidoc\structs\DataStructure('参数说明')));
// 添加附录
$apiDoc->getInfo()->addAppendix(new \BusyPHP\apidoc\structs\AppendixItem('附录1', '附录1描述'));
return $apiDoc->renderToHTML('XX项目接口文档');
}
}