On this page
搜索话题api
创建相关文件
php
// 创建搜索控制器
php think make:controller api/v1/Search
// 创建搜索验证层
php think make:validate SearchValidate
controller层:application\api\controller\v1\Search.php
php
<?php
namespace app\api\controller\v1;
use think\Controller;
use think\Request;
use app\common\controller\BaseController;
use app\common\validate\SearchValidate;
use app\common\model\Topic as TopicModel;
class Search extends BaseController
{
// 搜索话题
public function topic(){
(new SearchValidate())->goCheck();
$list = (new TopicModel())->Search();
return self::showResCode('获取成功',['list'=>$list]);
}
}
route层:route\route.php
php
Route::group('api/:v1/',function(){
// 搜索话题
Route::post('search/topic', 'api/v1.Search/topic');
});
validate层:application\common\validate\SearchValidate.php
php
protected $rule = [
'keyword'=>'require|chsDash',
'page'=>'require|integer|>:0',
];
model层:application\common\model\Topic.php
php
// 根据标题搜索话题
public function Search(){
// 获取所有参数
$param = request()->param();
return $this->where('title','like','%'.$param['keyword'].'%')->page($param['page'],10)->select();
}