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

获取文章详情api


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

php
// 文章详情
public function index()
{
    // 验证文章id
    (new PostValidate())->goCheck('detail');
    $detail = (new PostModel) -> getPostDetail();
    return self::showResCode('获取成功',['detail'=>$detail]);
}

route层:route\route.php

php
// 不需要验证token
Route::group('api/:version/',function(){
    ...
    // 获取文章详情
	Route::get('post/:id', 'api/v1.Post/index');
    ...
});

validate层:application\common\validate\PostValidate

php
protected $rule = [
    'id'=>'require|integer|>:0',
    ...
];
protected $scene = [
    ...
    'detail'=>['id']
];

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

php
// 关联用户表
public function user(){
    return $this->belongsTo('User');
}

// 关联分享
public function share(){
    return $this->belongsTo('Post','share_id','id');
}

// 获取文章详情
public function getPostDetail(){
    // 获取所有参数
    $param = request()->param();
    return $this->with(['user'=>function($query){
        return $query->field('id,username,userpic');
    },'images'=>function($query){
        return $query->field('url');
    },'share'])->find($param['id']);
}