Learn thinkphp5 article detail page interface development

Learn thinkphp5 article detail page interface development

Interface request controller

<?php

namespace app\api\controller;

use app\api\controller\Common;

use app\common\lib\exception\ApiException;


class News extends Common
{
    
    

    /**
     * 获取详情接口
     */
    public function read()
    {
    
    
        // 详情页面 APP -》 1、x.com/3.html  2、 接口
        $id = input('param.id', 0, 'intval');
        if (empty($id)) {
    
    
            return new ApiException('id is not ', 404);
        }

        // 通过id 去获取数据表里面的数据
        // try catch untodo
        $news = model('News')->get($id);
        if (empty($news) || $news->status != config('code.status_normal')) {
    
    
            return new ApiException('不存在该新闻', 404);
        }

        //修改文章的阅读数量自加1
        try {
    
    
            model('News')->where(['id' => $id])->setInc('read_count');
        } catch (\Exception $e) {
    
    
            return new ApiException('error', 400);
        }

        $cats = config('category.category_list');
        $news->catname = $cats[$news->catid];
        return show(config('code.success'), 'OK', $news, 200);
    }
}

postman request test

http://app.thinkphpwu.com/api/news/16

Insert picture description hereJust make up an id
http://app.thinkphpwu.com/api/news/16677 and it will report an error
Insert picture description here

Guess you like

Origin blog.csdn.net/guo_qiangqiang/article/details/112395618