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

指定话题分类的话题列表api


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

php
<?php

namespace app\api\controller\v1;

use think\Controller;
use think\Request;
use app\common\controller\BaseController;
use app\common\model\TopicClass as TopicClassModel;
use app\common\validate\TopicClassValidate;

class TopicClass extends BaseController
{
    ...

    // 获取指定话题分类下的话题列表
    public function topic(){
        // 验证分类id和分页数
        (new TopicClassValidate())->goCheck();
        
        $list=(new TopicClassModel)->getTopic();
        return self::showResCode('获取成功',['list'=>$list]);
    }
}

validate层:application\common\validate\TopicClassValidate.php

创建TopicValidate验证器

php
php think make:validate TopicClassValidate
php
protected $rule = [
    'id'=>'require|integer|>:0',
    'page'=>'require|integer|>:0',
];

route层:route\route.php

php
// 不需要验证token
Route::group('api/:version/',function(){
    ...
    // 获取指定话题分类下的话题列表
    Route::get('topicclass/:id/topic/:page', 'api/v1.TopicClass/topic');
    ...
});

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

php
// 关联话题
public function topic(){
    return $this->hasMany('Topic');
}
// 获取指定话题分类下的话题(分页)
public function getTopic(){
    // 获取所有参数
    $param = request()->param();
    return self::get($param['id'])->topic()->page($param['page'],10)->select();
}