buildAdmin 后端控制器的代码分析

buildAdmin的代码生成,很像是 fastadmin 的生成模式,当我们利用数据库生成了一个控制器的时候,我们可以看到, 它的生成代码很简洁


<?phpnamespace app\admin\controller\askanswer;use app\common\controller\Backend;/*** 回答管理*/
class Answer extends Backend     //控制器继承了 backend
{/*** Answer模型对象* @var object* @phpstan-var \app\admin\model\Answer*///定义了一个 模型对象,也就是对应数据表的 模型protected object $model;//这里是在添加数据中 排除了一些,自动生成的字段,  在Backend中,有对这些字段的调用protected array|string $preExcludeFields = ['id', 'create_time', 'update_time'];//这里是设置了前端的快速搜索protected string|array $quickSearchField = ['id'];public function initialize(): void{parent::initialize(); //这里用了父类的 初始化方法, 做了一些 用户认证,权限判断,数据库连接检测等$this->model = new \app\admin\model\Answer;$this->request->filter('clean_xss');  //这里对 request 做了一次 xss 攻击的过滤}/*** 若需重写查看、编辑、删除等方法,请复制 @see \app\admin\library\traits\Backend 中对应的方法至此进行重写*/
}

接着我们来到,父类, backend
在这里插入图片描述
在追代码的过程中,我没有看到 跨域的操作, 因为fastadmin 在这里面是有跨域操作的一段代码的,后来经过 整块代码搜索, 才想起来, 这是tp8了, 是有中间键的,而fastadmin中是tp5.0,没有中间键的
在这里插入图片描述
真正的 增,删,改,查的代码 就在traits中

<?phpnamespace app\admin\library\traits;use Throwable;
use think\facade\Config;/*** 后台控制器trait类* 已导入到 @see \app\common\controller\Backend 中* 若需修改此类方法:请复制方法至对应控制器后进行重写*/
trait Backend
{/*** 排除入库字段* @param array $params* @return array*/protected function excludeFields(array $params): array{if (!is_array($this->preExcludeFields)) {$this->preExcludeFields = explode(',', (string)$this->preExcludeFields);}foreach ($this->preExcludeFields as $field) {if (array_key_exists($field, $params)) {unset($params[$field]);}}return $params;}/*** 查看* @throws Throwable*/public function index(): void{if ($this->request->param('select')) {$this->select();}list($where, $alias, $limit, $order) = $this->queryBuilder();$res = $this->model->field($this->indexField)->withJoin($this->withJoinTable, $this->withJoinType)->alias($alias)->where($where)->order($order)->paginate($limit);$this->success('', ['list'   => $res->items(),'total'  => $res->total(),'remark' => get_route_remark(),]);}/*** 添加*/public function add(): void{if ($this->request->isPost()) {$data = $this->request->post();if (!$data) {$this->error(__('Parameter %s can not be empty', ['']));}$data = $this->excludeFields($data);if ($this->dataLimit && $this->dataLimitFieldAutoFill) {$data[$this->dataLimitField] = $this->auth->id;}$result = false;$this->model->startTrans();try {// 模型验证if ($this->modelValidate) {$validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));if (class_exists($validate)) {$validate = new $validate;if ($this->modelSceneValidate) $validate->scene('add');$validate->check($data);}}$result = $this->model->save($data);$this->model->commit();} catch (Throwable $e) {$this->model->rollback();$this->error($e->getMessage());}if ($result !== false) {$this->success(__('Added successfully'));} else {$this->error(__('No rows were added'));}}$this->error(__('Parameter error'));}/*** 编辑* @throws Throwable*/public function edit(): void{$id  = $this->request->param($this->model->getPk());$row = $this->model->find($id);if (!$row) {$this->error(__('Record not found'));}$dataLimitAdminIds = $this->getDataLimitAdminIds();if ($dataLimitAdminIds && !in_array($row[$this->dataLimitField], $dataLimitAdminIds)) {$this->error(__('You have no permission'));}if ($this->request->isPost()) {$data = $this->request->post();if (!$data) {$this->error(__('Parameter %s can not be empty', ['']));}$data   = $this->excludeFields($data);$result = false;$this->model->startTrans();try {// 模型验证if ($this->modelValidate) {$validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));if (class_exists($validate)) {$validate = new $validate;if ($this->modelSceneValidate) $validate->scene('edit');$validate->check($data);}}$result = $row->save($data);$this->model->commit();} catch (Throwable $e) {$this->model->rollback();$this->error($e->getMessage());}if ($result !== false) {$this->success(__('Update successful'));} else {$this->error(__('No rows updated'));}}$this->success('', ['row' => $row]);}/*** 删除* @param array $ids* @throws Throwable*/public function del(array $ids = []): void{if (!$this->request->isDelete() || !$ids) {$this->error(__('Parameter error'));}$where             = [];$dataLimitAdminIds = $this->getDataLimitAdminIds();if ($dataLimitAdminIds) {$where[] = [$this->dataLimitField, 'in', $dataLimitAdminIds];}$pk      = $this->model->getPk();$where[] = [$pk, 'in', $ids];$count = 0;$data  = $this->model->where($where)->select();$this->model->startTrans();try {foreach ($data as $v) {$count += $v->delete();}$this->model->commit();} catch (Throwable $e) {$this->model->rollback();$this->error($e->getMessage());}if ($count) {$this->success(__('Deleted successfully'));} else {$this->error(__('No rows were deleted'));}}/*** 排序* @param int $id       排序主键值* @param int $targetId 排序位置主键值* @throws Throwable*/public function sortable(int $id, int $targetId): void{$dataLimitAdminIds = $this->getDataLimitAdminIds();if ($dataLimitAdminIds) {$this->model->where($this->dataLimitField, 'in', $dataLimitAdminIds);}$row    = $this->model->find($id);$target = $this->model->find($targetId);if (!$row || !$target) {$this->error(__('Record not found'));}if ($row[$this->weighField] == $target[$this->weighField]) {$autoSortEqWeight = is_null($this->autoSortEqWeight) ? Config::get('buildadmin.auto_sort_eq_weight') : $this->autoSortEqWeight;if (!$autoSortEqWeight) {$this->error(__('Invalid collation because the weights of the two targets are equal'));}// 自动重新整理排序$all = $this->model->select();foreach ($all as $item) {$item[$this->weighField] = $item[$this->model->getPk()];$item->save();}unset($all);// 重新获取$row    = $this->model->find($id);$target = $this->model->find($targetId);}$backup                    = $target[$this->weighField];$target[$this->weighField] = $row[$this->weighField];$row[$this->weighField]    = $backup;$row->save();$target->save();$this->success();}/*** 加载为select(远程下拉选择框)数据,默认还是走$this->index()方法* 必要时请在对应控制器类中重写*/public function select(): void{}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/202272.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

数据中台之用户画像

用户画像应用领域较为广泛,适合于各个产品周期,从新用户的引流到潜在用户的挖掘、 从老用户 的培养到流失用户的回流等。通过挖掘用户兴趣、偏好、人口统计特征,可以 直接 作用于提升营销精准 度、推荐匹配度,最终提升产品服务和企业利润。还包括广告投放、产品布局和行业报…

webshell之无扩展免杀

1.php加密 这里是利用phpjiami网站进行加密&#xff0c;进而达到加密效果 加密前&#xff1a; 查杀效果 可以看到这里D某和某狗都查杀 里用php加密后效果 查杀效果 可以看到这里只有D某会显示加密脚本&#xff0c;而某狗直接绕过 2.dezend加密 可以看到dezend加密的特征还是…

SA实战 ·《SpringCloud Alibaba实战》第14章-服务网关加餐:SpringCloud Gateway核心技术

大家好,我是冰河~~ 一不小心《SpringCloud Alibaba实战》专栏都更新到第14章了,再不上车就跟不上了,小伙伴们快跟上啊! 在《SpringCloud Alibaba实战》专栏前面的文章中,我们实现了用户微服务、商品微服务和订单微服务之间的远程调用,并且实现了服务调用的负载均衡。也基…

联想拯救者Lenovo Legion R9000K 2021H(82N6)原装出厂Windows10/Win11系统ISO镜像

链接&#xff1a;https://pan.baidu.com/s/13NkeCXNdV0Ib5eeRnZUeAQ?pwdnlr7 提取码&#xff1a;nlr7 拯救者笔记本电脑原厂WIN系统自带所有驱动、出厂主题壁纸、系统属性专属LOGO标志、Office办公软件、联想电脑管家等预装程序 所需要工具&#xff1a;16G或以上的U盘 文…

第15届蓝桥杯Scratch选拔赛中级(STEMA)真题2023年10月

一、单选题 1.运行以下哪个程序后&#xff0c;巨嘴鸟会向下移动&#xff1f;&#xff08; &#xff09; A. B. C. D. 2.运行以下程序后&#xff0c; 能看到几只河豚鱼&#xff08; &#xff09;&#xff1f; A.3 B.4 C.6 D.7 3.以下运算结果为“False”的是&#xff08…

财报解读:电商GMV增长30%后,快手将坚守本地生活?

快手逐渐讲好了其高质量成长的故事。 根据财报&#xff0c;快手三季度业绩超出预期&#xff0c;其中&#xff0c;营收279.5亿元&#xff0c;同比增长20.8%&#xff1b;调整后净利润31.7亿元&#xff0c;同比扭亏为盈。 而联系市场环境来看&#xff0c;三季度广告、电商市场较…

webpack环境变量的设置

现在虽然vite比较流行&#xff0c;但对于用node写后端来说&#xff0c;webpack倒是成了一个很好的打包工具&#xff0c;可以很好的保护后端的代码。所以这块的学习还是不能停下来&#xff0c;接下来我们来针对不同的环境做不同的设置写好笔记。 引用场景主要是针对服务器的各种…

设计师不能忽视的几个宝藏图标设计工具

在这个快速变化的时代&#xff0c;设计师对创新和实用工具的需求越来越大。这就要求我们及时跟上潮流&#xff0c;不断探索和尝试最新、最有价值的图标设计工具。只有这样&#xff0c;我们才能在竞争激烈的设计市场中脱颖而出。以下是我们精心挑选的2024年值得一试的图标设计工…

MySQL数据库:开源且强大的关系型数据库管理系统

大家好&#xff0c;我是咕噜-凯撒&#xff0c;数据在当今信息化时代的重要性不可忽视。作为企业和组织的重要资产&#xff0c;数据的管理和存储变得至关重要&#xff0c;MySQL作为一种关系型数据库管理系统&#xff0c;具有非常多的优势&#xff0c;下面简单的探讨一下MySQL数据…

labview 安捷伦 34970A 采集温度等

本文详细描述了怎么用安捷伦34970A采集温度&#xff0c;并列出了labview的下载链接&#xff0c;具有一定的参考价值。 1.必要条件&#xff1a; RS-232电缆一根 IO Libraries Suite 软件 BenchLink Data Logger 软件 软件可以在http://www.keysight.com.cn下载 检查RS-232…

Hive安装配置 - 本地模式

文章目录 一、Hive运行模式二、安装配置本地模式Hive&#xff08;一&#xff09;安装配置MySQL1、删除系统自带的MariaDB2、上传MySQL组件到虚拟机3、在主节点上安装MySQL组件4、在主节点上配置MySQL&#xff08;1&#xff09;查看MySQL服务状态&#xff08;2&#xff09;查看M…

笔记本外接显示器的一些基本操作

1>&#xff0c;安装问题直接问客服&#xff0c;正常情况是将显示屏接上电源&#xff0c;然后用先将显示屏和笔记本的HDMI接口连接即可。 按下组合键 win p ,选择 “复制”。 2>&#xff0c;接上显示屏后&#xff0c;原笔记本无声音&#xff1f; 1、找到笔记本电脑右下…

2023“亚太杯”大学生数学建模竞赛

2023亚太杯数学建模C题 中国新能源电动汽车的发展趋势 解题思路、数据 该题并没有提供数据集&#xff0c;对所需数据进行收集整理是对题目进行求解的基础。在本题中&#xff0c;主要需要以下数据&#xff1a;新能源汽车历史销售量、新能汽车相关专利的历史数量、充电桩历史数…

使用whisper实现语音转文本

项目地址&#xff1a;GitHub - openai/whisper: Robust Speech Recognition via Large-Scale Weak Supervision 1、需要py3.8环境 conda activate p38 2、安装 pip install -U openai-whisper 3、下载项目 pip install githttps://github.com/openai/whisper.git 4、安装…

从六个方面对比Go和Python的差异

您是否想过 Go 与 Python 之间的主要区别是什么&#xff1f;随着对软件开发人员的需求不断增加&#xff0c;选择哪种编码语言可能会很困难。 ​ 在此&#xff0c;我们将从六个方面对比Go和Python,探讨 Go 和 Python之间的差异。我们将讨论它们的特点、优缺点&#xff0c;以便…

如何避免Steam搬砖项目中账号被盗

购买steam余额有风险吗&#xff1f;及N种被红锁的情况 相信最近很多人都已经听说过steam游戏搬砖这个项目&#xff0c;也叫CSGO游戏搬砖项目&#xff0c;还有人叫它&#xff1a;国外steam游戏汇率差项目&#xff0c;无论怎么称呼&#xff0c;都是同一个项目。 那么什么是stea…

HarmonyOS ArkTS开发语言介绍(三)

1 引言 Mozilla创造了JS&#xff0c;Microsoft创建了TS&#xff0c;Huawei进一步推出了ArkTS。 从最初的基础的逻辑交互能力&#xff0c;到具备类型系统的高效工程开发能力&#xff0c;再到融合声明式UI、多维状态管理等丰富的应用开发能力&#xff0c;共同组成了相关的演进脉…

Threejs_07 环境、透明度、纹理、ao、光照等贴图的渲染

老陈打码 继续学习老陈threejs 支持&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01; 下面用到的所有图片、资源、hdr文件都是老陈打码的原资源 链接&#xff1a;https://pan.baidu.com/s/1WWWHgekCIH7OnjI7S_3ZtQ 提取码&#xff1a;6666 Thre…

Flink Operator 使用指南 之 Flink Operator安装

介绍 Flink Kubernetes Operator 充当控制平面来管理 Apache Flink 应用程序的完整部署生命周期。尽管 Flink 的Native Kubernetes 集成已经允许用户在运行的 Kubernetes(k8s) 集群上直接部署 Flink 应用程序,但自定义资源和Operator Pattern 也已成为 Kubernetes 原生部署体…

一篇五分生信临床模型预测文章代码复现——Figure 10.机制及肿瘤免疫浸润(六)

之前讲过临床模型预测的专栏,但那只是基础版本,下面我们以自噬相关基因为例子,模仿一篇五分文章,将图和代码复现出来,学会本专栏课程,可以具备发一篇五分左右文章的水平: 本专栏目录如下: Figure 1:差异表达基因及预后基因筛选(图片仅供参考) Figure 2. 生存分析,…