real-apidoc基于 tp6 的 api 生成模塊
基于 tp6 的 api 生成模塊,參考 fastadmin 的 api 模塊
引用
下載到 tp6 的extend\jmwl下
use jmwl\realapidoc\Builder; use think\facade\View;
使用
這里是所有的需要生成api的控制器路徑啦、所以,你可以先獲取到模塊下的所有控制器 Builder(["\app\admin\controller\Demo","\app\admin\controller\Index"])
$title="類名"; $url="http://real-think.jmwl51.com"; $lang=[ 'Info' => '基礎(chǔ)信息', 'Sandbox' => '在線測試', 'Sampleoutput' => '返回示例', 'Headers' => 'Headers', 'Parameters' => '參數(shù)', 'Body' => '正文', 'Name' => '名稱', 'Type' => '類型', 'Required' => '必選', 'Description' => '描述', 'Send' => '提交', 'Reset' => '重置', 'Tokentips' => 'Token在會員注冊或登錄后都會返回,WEB端同時存在于Cookie中', 'Apiurltips' => 'API接口URL', 'Savetips' => '點擊保存后Token和Api url都將保存在本地Localstorage中', 'Authorization' => '權(quán)限', 'NeedLogin' => '登錄', 'NeedRight' => '鑒權(quán)', 'ReturnHeaders' => '響應(yīng)頭', 'ReturnParameters' => '返回參數(shù)', 'Response' => '響應(yīng)輸出', ]; $config = [ 'sitename' => "測試", 'title' => $title, 'author' => "測試", 'description' => '', 'apiurl' => $url, 'language' => $lang, ]; //這里是所有的需要生成api的控制器路徑啦 $builder = new Builder(["\\app\\admin\\controller\\Demo","\\app\\admin\\controller\\Index"]); $content = $builder->render(root_path()."/extend/jmwl/realapidoc/template/index.html", ['config' => $config, 'lang' => $lang]); // dump($content); die; View::assign('config',$config); View::assign('docslist',$content["docsList"]); View::assign('lang',$content["lang"]); return View();
視圖
文件中的index.html就是視圖啦,放到正確的位置 我放在了 app\admin\view\api\index.html 對應(yīng)的控制器自然是 admin下的api控制器啦
控制器注釋
| 名稱 | 描述 | 示例 |
|---|---|---|
| @ApiSector | API分組名稱 | (測試分組) |
| @ApiRoute | API接口URL,此@ApiRoute只是基礎(chǔ)URL | (/api/test) |
| @ApiInternal | 忽略的控制器,表示此控制將不加入API文檔 | 無 |
| @ApiWeigh | API方法的排序,值越大越靠前 | (99) |
控制器方法注釋
| 名稱 | 描述 | 示例 |
|---|---|---|
| @ApiTitle | API接口的標題,為空時將自動匹配注釋的文本信息 | (測試標題) |
| @ApiSummary | API接口描述 | (測試描述) |
| @ApiRoute | API接口地址,為空時將自動計算請求地址 | (/api/test/index) |
| @ApiMethod | API接口請求方法,默認為GET | (POST) |
| @ApiSector | API分組,默認按鈕控制器或控制器的@ApiSector進行分組 | (測試分組) |
| @ApiParams | API請求參數(shù),如果在@ApiRoute中有對應(yīng)的{@參數(shù)名},將進行替換 | (name="id", type="integer", required=true, description="會員ID") |
| @ApiHeaders | API請求傳遞的Headers信息 | (name=token, type=string, required=true, description="請求的Token") |
| @ApiReturn | API返回的結(jié)果示例 | ({"code":1,"msg":"返回成功"}) |
| @ApiReturnParams | API返回的結(jié)果參數(shù)介紹 | (name="code", type="integer", required=true, sample="0") |
| @ApiReturnHeaders | API返回的Headers信息 | (name="token", type="integer", required=true, sample="123456") |
| @ApiInternal | 忽略的方法,表示此方法將不加入文檔 | 無 |
| @ApiWeigh | API方法的排序,值越大越靠前 | (99) |
示范
<?php namespace app\api\controller; /** * 測試API控制器 */ class Test extends \app\common\controller\Api { // 無需驗證登錄的方法 protected $noNeedLogin = ['test']; // 無需要判斷權(quán)限規(guī)則的方法 protected $noNeedRight = ['*']; /** * 首頁 * * 可以通過@ApiInternal忽略請求的方法 * @ApiInternal */ public function index() { return 'index'; } /** * 私有方法 * 私有的方法將不會出現(xiàn)在文檔列表 */ private function privatetest() { return 'private'; } /** * 測試方法 * * @ApiTitle (測試名稱) * @ApiSummary (測試描述信息) * @ApiSector (測試分組) * @ApiMethod (POST) * @ApiRoute (/api/test/test/id/{id}/name/{name}) * @ApiHeaders (name=token, type=string, required=true, description="請求的Token") * @ApiParams (name="id", type="integer", required=true, description="會員ID") * @ApiParams (name="name", type="string", required=true, description="用戶名") * @ApiParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="擴展數(shù)據(jù)") * @ApiReturnParams (name="code", type="integer", required=true, sample="0") * @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功") * @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="擴展數(shù)據(jù)返回") * @ApiReturn ({ 'code':'1', 'mesg':'返回成功' * }) */ public function test($id = '', $name = '') { $this->success("返回成功", $this->request->request()); } }
評論
圖片
表情
