php怎么实现关注功能
发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,php怎么实现关注功能,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。php实现关注功能的方法:1、创建控制层实现代码"n
千家信息网最后更新 2026年11月03日php怎么实现关注功能
php怎么实现关注功能,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
php实现关注功能的方法:1、创建控制层实现代码"namespace App\Controller\Test...";2、设计服务层实现代码"namespace App\Service\Ptg...";3、设置好仓储层代码即可。
本文操作环境:windows7系统、PHP7.1版、DELL G3电脑
php怎么实现关注功能?
php + redis 实现关注功能:
产品价值
1: 关注功能
2: 功能分析之"关注"功能
3: 平平无奇的「关注」功能,背后有4点重大价值
应用场景
在做PC或者APP端时,掺杂点社交概念就有关注和粉丝功能;
数据量小的话数据库还能支持,如果数据量很庞大还是用缓存比较好。
具体实现
1 控制层实现
input('type', 'follow'); // 1-关注-follow 2-取消关注-remove $userId = $request->input('user_id', 0); // 我的用户ID $otherId = $request->input('other_id', 0); // 我关注的用户ID if ($userId == $otherId) { return $this->response->apiResponse(); } $this->testFollowService->follow($type, $userId, $otherId); return $this->response->apiResponse(); } /** * 我的关注/粉丝 * @param Request $request * @return mixed */ public function myFollowAndFans(Request $request) { $type = $request->input('type', 'follow'); // 1-关注-follow 2-粉丝-fans $userId = $request->input('user_id', 0); // 我的用户ID $page = $request->input('page', 1); // 页码 $limit = $request->input('limit', 10); // 每页显示条数 $res = $this->testFollowService->myFollowAndFans($userId, $type, $page, $limit); return $this->response->apiResponse($res); }}?>2 服务层实现
testFollowRedis->zAddFollow($userId, $otherId); $this->testFollowRedis->zAddFans($otherId, $userId); } // 取消关注 if ($type === 'remove') { // 先处理 mysql // TODO mysql 操作 // 然后处理 redis $this->testFollowRedis->zRemFollow($userId, $otherId); $this->testFollowRedis->zRemFans($otherId, $userId); } } /** * 我的关注/粉丝 * @param int $userId 当前登录用户的ID * @param string $type 要获取的数据 * @param int $page 页码 * @param int $limit 限制条数 * @return array */ public function myFollowAndFans(int $userId, $type = 'follow', $page = 1, $limit = 10) { $start = $limit * ($page - 1); $end = $start + $limit - 1; $res = []; if ($type === 'follow') { $res = $this->testFollowRedis->zRangeFollow($userId, $start, $end); } if ($type === 'fans') { $res = $this->testFollowRedis->zRangeFans($userId, $start, $end); } return $res; }}?>仓储层实现【推荐:PHP视频教程】
redis->zAdd(sprintf($this->prefix . $this->followKey, $userId), time(), $otherId); } /** * 取消关注 * @param $userId * @param $otherId */ public function zRemFollow($userId, $otherId) { $this->redis->zRem(sprintf($this->prefix . $this->followKey, $userId), $otherId); } /** * 我的关注 | 正序 * @param int $userId * @param int $start * @param int $end * @return array */ public function zRangeFollow(int $userId, int $start = 0, int $end = 9) { return $this->redis->zRange(sprintf($this->prefix . $this->followKey, $userId), $start, $end); } /** * 我的关注 | 倒序 * @param int $userId * @param int $start * @param int $end * @return array */ public function zRevRangeFollow(int $userId, int $start = 0, int $end = 9) { return $this->redis->zRevRange(sprintf($this->prefix . $this->followKey, $userId), $start, $end); } /** * 增加粉丝 * @param $userId * @param $otherId */ public function zAddFans($userId, $otherId) { $this->redis->zAdd(sprintf($this->prefix . $this->fansKey, $userId), time(), $otherId); } /** * 移除粉丝 * @param $userId * @param $otherId */ public function zRemFans($userId, $otherId) { $this->redis->zRem(sprintf($this->prefix . $this->fansKey, $userId), $otherId); } /** * 我的粉丝 | 正序 * @param int $userId * @param int $start * @param int $end * @return array */ public function zRangeFans(int $userId, int $start = 0, int $end = 9) { return $this->redis->zRange(sprintf($this->prefix . $this->fansKey, $userId), $start, $end); } /** * 我的粉丝 | 倒序 * @param int $userId * @param int $start * @param int $end * @return array */ public function zRevRangeFans(int $userId, int $start = 0, int $end = 9) { return $this->redis->zRevRange(sprintf($this->prefix . $this->fansKey, $userId), $start, $end); }}看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注行业资讯频道,感谢您对的支持。
功能
粉丝
数据
用户
代码
价值
倒序
页码
仓储
处理
帮助
控制
支持
服务
清楚
重大
产品
内容
场景
对此
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网科技活动banner
科技互联网龙头
中国互联网科技股
今日互联网科技
互联网金融 科技
上海挚品互联网科技
南京欣网互联网科技
互联网科技公司招聘
恒银科技与互联网金融
北京华百购互联网科技有限公司
世界互联网科技公司市值排名
互联网科技公司实习生
互联网数据科技专业
黑云互联网科技有限公司
领航科技大厦互联网公司
国产互联网科技公司
广西科技产业互联网是什么
陕西九四连强互联网科技有限公司
上海万顺科技互联网有限公司招聘
北京品多多互联网科技有限公司
互联网通讯科技
工业互联网科技新基建
科技类互联网
成都互联网科技公司
远拓互联网科技
互联网科技与大数据专业
广州互联网高科技取餐柜
同城互联网络科技
众联互联网络科技
黑龙江互联网科技公司