php做一个详情页面,[PHP高可用后端]③⑥--新闻详情页面接口开发

d38e40e36c2dd0766f07c421f168d134.png

微信截图_20171124112446.png

a6809b130f7066ee52c0107a965ab504.png

微信截图_20171124135000.png

e667ab488ea9063adb888d2fd1d245d8.png

微信截图_20171124135428.png

News.php

/**

* Created by PhpStorm.

* User: tong

* Date: 2017/11/23

* Time: 17:03

*/

namespace app\api\controller\v1;

use app\api\controller\Common;

use app\common\lib\exception\ApiException;

class News extends Common

{

public function index()

{

//仿照之前讲解的validate验证机制 做相关检验

$data = input('get.');

$whereData['status'] = config('code.status_normal');

if (!empty($data['catid'])) {

$whereData['catid'] = input('get.catid', 0, 'intval');

}

if (!empty($data['title'])) {

$whereData['title'] = ['like', '%' . $data['title'] . '%'];

}

$this->getPageAndSize($data);

$total = model('News')->getNewsByCountCondition($whereData);

$news = model('News')->getNewsByCondition($whereData, $this->from, $this->size);

$result = [

'total' => $total,

'page_num' => ceil($total / $this->size),

'list' => $this->getDealNews($news),

];

return show(1, 'OK', $result, 200);

}

/**

* 获取详情接口

*/

public function read()

{

//详情页面 APP -》1.x.com/3.html 2.接口

$id = input('param.id', 0, 'intval');

if (empty($id)) {

new ApiException('id is not ', 404);

}

//通过id 去获取数据表里的数据

try {

$news = model('News')->get($id);

} catch (\Exception $e) {

return new ApiException($e->getMessage(), 400);

}

if (empty($news) || $news->status != config('code.status_normal')) {

return new ApiException('不存在该新闻', 404);

}

try {

model('News')->where(['id' => $id])->setInc('read_count');

} catch (\Exception $e) {

return new ApiException($e->getMessage(), 400);

}

$cats = config('cat.list');

$news->catname = $cats[$news->catid];

return show(config('code.success'), 'OK', $news, 200);

}

}

c96f731f8a1752f99e38919af37b3d10.png

image.png

本站以现代、古代情诗为主,情诗网创办于2013年,以原创爱情诗歌、经典情诗、现代情诗、古代情诗、英文情诗、情诗绝句为主并收集古诗、古诗词、诗歌大全、诗词名句的文学门户。方便您下次继续阅读;可以放在浏览器的收藏夹中(快捷键Ctrl+D);或者看到喜欢或者有趣的诗词可以通过分享按钮给你的好友分享;情诗网是目前最全情诗大全网站之一。并欢迎广大诗歌爱好者阅览投稿!喜欢本站的话请大家把本站告诉给你朋友哦!地址是 www.qingshiwang.com !

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值