Skip to content
关注公众号,获取新课通知
【重要通知】uniapp实战社区交友交流群更换为:602180461,靓仔/靓女可以重新申请加入哦~

获取10条热门话题api


创建话题相关文件

php
// 创建话题控制器
php think make:controller api/v1/Topic
// 创建话题模型
php think make:model Topic

controller层:application\api\controller\v1\Topic.php

php
<?php

namespace app\api\controller\v1;

use think\Controller;
use think\Request;
use app\common\model\Topic as TopicModel;
use app\common\controller\BaseController;

class Topic extends BaseController
{
    // 获取10个话题
    public function index()
    {
        $list = (new TopicModel())->gethotlist();
        return self::showResCode('获取成功',['list'=>$list]);
    }
    
    ...
}

route层:route\route.php

php
// 不需要验证token
Route::group('api/:version/',function(){
    ...
    // 获取热门话题
	Route::get('hottopic','api/v1.Topic/index');
    ...
});

model层:application\common\model\Topic.php

php
// 获取热门话题列表
public function gethotlist(){
    return $this->where('type',1)->limit(10)->select()->toArray();
}