tp5.1 使用apidoc


1.  安装tp5.1
2. 安装apidoc插件

打开浏览器访问 http://你的域名/apidoc/ ,出现接口文档页面,表示安装成功。

如出现404错误 请访问以下链接排错:

页面404错误 | ThinkPHP-ApiDoc

简单使用: 

以 app\index\controller\index 控制器为例

<?php
namespace app\index\controller;
// 添加这句,注释写法为 @Apidoc\参数名(...)
use hg\apidoc\annotation as Apidoc;

/**
 * @Apidoc\Title("首页控制器")
 * @Apidoc\Group("index")
 */

class Index
{
    public function index()
    {
        return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) 2018新年快乐</h1><p> ThinkPHP V5.1<br/><span style="font-size:30px">12载初心不改(2006-2018) - 你值得信赖的PHP框架</span></p></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="eab4b9f840753f8e7"></think>';
    }

    /**
     * @Apidoc\Title("hello 方法")
     * @Apidoc\Desc("最基础的接口注释写法")
     * @Apidoc\Method("GET")
     * @Apidoc\Tag("测试")
     * @Apidoc\Param("name", type="string",require=false, desc="用户名")
     * @Apidoc\Returned("data", type="string", desc="返回数据1",replaceGlobal=true),
     */
    public function hello($name = 'ThinkPHP5')
    {
        return 'hello,' . $name;
    }
}

修改配置文件 config/apidoc.php

<?php
return [
    // 文档标题
    'title'              => 'API接口文档',
    // 文档描述
    'desc'               => '文档描述',
    // 默认请求类型
    'default_method'=>'GET',
    // 允许跨域访问
    'allowCrossDomain'=>false,
    // 设置可选版本
    // 'apps'           => [],
    'apps' => [
        [
            'title' => '首页管理',
            'path' => 'app\index\controller',
            'folder' => 'index',
            'groups'  => [
                ['title' => '基础模块', 'name' => 'index'],
                // ['title' => '示例模块', 'name' => 'demo'],
                // [
                //     'title' => '多级模块', 'name' => 'subMenu',
                //     'children' => [
                //         ['title' => '多级v1', 'name' => 'subv1',],
                //         ['title' => '多级v2', 'name' => 'subv2'],
                //     ]
                // ],
            ],
            'controllers'=>[
                'app\index\controller\Index',
                // 'app\admin\controller\CrudDemo',
            ],
            // 'headers'=>[
            //     ['name'=>'token','type'=>'string','desc'=>'admin应用的全局请求头参数token'],
            // ],
            // 'parameters'=>[
            //     ['name'=>'abc','type'=>'string','desc'=>'admin应用的全局请求体参数abc'],
            // ],
        ],
        // [
        //     'title' => '演示示例',
        //     'path' => 'app\demo\controller',
        //     'folder' => 'demo',
        //     'items' => [
        //         ['title' => 'V1.0', 'path' => 'app\demo\controller\v1', 'folder' => 'v1'],
        //         ['title' => 'V2.0', 'path' => 'app\demo\controller\v2', 'folder' => 'v2']
        //     ],

        // ],
    ],
    // 自动生成url规则
    'auto_url' => [
        // 字母规则
        'letter_rule' => "lcfirst",
        // 多级路由分隔符
        'multistage_route_separator'  =>"."
    ],
    // 指定公共注释定义的文件地址
    'definitions'        => "app\common\controller\Definitions",
    // 缓存配置
    'cache'              => [
        // 是否开启缓存
        'enable' => false,
    ],
    // 权限认证配置
    'auth'               => [
        // 是否启用密码验证
        'enable'     => true,
        // 全局访问密码
        'password'   => "123456",
        // 密码加密盐
        'secret_key' => "apidoc#hg_code",
        // 有效期
        'expire' => 24*60*60
    ],
    // 统一的请求Header
    'headers'=>[],
    // 统一的请求参数Parameters
    'parameters'=>[],
    // 统一的请求响应体
    'responses'=>[
        ['name'=>'code','desc'=>'代码','type'=>'int'],
        ['name'=>'message','desc'=>'业务信息','type'=>'string'],
        ['name'=>'data','desc'=>'业务数据','main'=>true,'type'=>'object'],
    ],
    // md文档
    'docs'              => [],

];

groups 须与 控制器中的注解 groups一一对应

 

至此完成!

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值