On this page
搜索文章api
controller层:application\api\controller\v1\Search.php
php
use app\common\model\Post as PostModel;
...
// 搜索文章
public function post(){
(new SearchValidate())->goCheck();
$list = (new PostModel())->Search();
return self::showResCode('获取成功',['list'=>$list]);
}
route层:route\route.php
php
Route::group('api/:v1/',function(){
// 搜索文章
Route::post('search/post', 'api/v1.Search/post');
});
model层:application\common\model\Post.php
php
// 根据标题搜索文章
public function Search(){
// 获取所有参数
$param = request()->param();
return $this->where('title','like','%'.$param['keyword'].'%')->with(['user'=>function($query){
return $query->field('id,username,userpic');
},'images'=>function($query){
return $query->field('url');
},'share'])->page($param['page'],10)->select();
}