找回密码
 注册
首页 ≡≡网络技术≡≡ PHP语言 FastAdmin基类控制器简介

框架 FastAdmin基类控制器简介

灰儿 2022-7-6 09:19:16
FastAdmin控制器都必须继承自 application/common/controller/Backend.php的\app\common\controller\Backend 这个基类,这个基类application/admin/library/traits/Backend.php 里引入的 use \app\admin\library\traits\Backend;它有八个公共方法,
index/add/edit/del/multi/recyclebin/destroy/restore/ import
查看/添加/编辑/删除/批量更新/回收站/真实删除/还原/导入

common/controller/Backend里还有好多属性:
  1. /**
  2. * 无需登录的方法,同时也就不需要鉴权了
  3. * @var array
  4. */
  5. protected $noNeedLogin = [];

  6. /**
  7. * 无需鉴权的方法,但需要登录
  8. * @var array
  9. */
  10. protected $noNeedRight = [];

  11. /**
  12. * 布局模板
  13. * @var string
  14. */
  15. protected $layout = 'default';

  16. /**
  17. * 权限控制类
  18. * @var Auth
  19. */
  20. protected $auth = null;

  21. /**
  22. * 快速搜索时执行查找的字段
  23. */
  24. protected $searchFields = 'id';

  25. /**
  26. * 是否是关联查询
  27. */
  28. protected $relationSearch = false;

  29. /**
  30. * 是否开启数据限制
  31. * 支持auth/personal
  32. * 表示按权限判断/仅限个人
  33. * 默认为禁用,若启用请务必保证表中存在admin_id字段
  34. */
  35. protected $dataLimit = false;

  36. /**
  37. * 数据限制字段
  38. */
  39. protected $dataLimitField = 'admin_id';

  40. /**
  41. * 是否开启Validate验证
  42. */
  43. protected $modelValidate = false;

  44. /**
  45. * 是否开启模型场景验证
  46. */
  47. protected $modelSceneValidate = false;

  48. /**
  49. * Multi方法可批量修改的字段
  50. */
  51. protected $multiFields = 'status';

  52. 方法

  53. /**
  54. * 加载语言文件
  55. * @param string $name
  56. */
  57. protected function loadlang($name)
  58. {
  59. }

  60. /**
  61. * 渲染配置信息
  62. * @param mixed $name 键名或数组
  63. * @param mixed $value 值
  64. */
  65. protected function assignconfig($name, $value = '')
  66. {
  67. }

  68. /**
  69. * 生成查询所需要的条件,排序方式
  70. * @param mixed $searchfields 快速查询的字段
  71. * @param boolean $relationSearch 是否关联查询
  72. * @return array
  73. */
  74. protected function buildparams($searchfields = null, $relationSearch = null)
  75. {
  76. }

  77. /**
  78. * 获取数据限制的管理员ID
  79. * 禁用数据限制时返回的是null
  80. * @return mixed
  81. */
  82. protected function getDataLimitAdminIds()
  83. {
  84. }

  85. /**
  86. * Selectpage的实现方法
  87. *
  88. * 当前方法只是一个比较通用的搜索匹配,请按需重载此方法来编写自己的搜索逻辑,$where按自己的需求写即可
  89. * 这里示例了所有的参数,所以比较复杂,实现上自己实现只需简单的几行即可
  90. *
  91. */
  92. protected function selectpage()
  93. {
  94. }
复制代码

当两张相似的表意见curd时一个没有关联模型查询一个有时的区别:
  1. class Demo extends Backend
  2. {

  3.     /**
  4.      * Demo模型对象
  5.      * @var \app\admin\model\Demo
  6.      */
  7.     protected $model = null;

  8.     public function _initialize()
  9.     {
  10.         parent::_initialize();
  11.         $this->model = new \app\admin\model\Demo;

  12.     }

  13.     /**
  14.      * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  15.      * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  16.      * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  17.      */

  18. }
复制代码

traits  的原idnex方法
  1. trait Backend
  2. {

  3.     /**
  4.      * 查看
  5.      */
  6.     public function index()
  7.     {
  8.         //设置过滤方法
  9.         $this->request->filter(['strip_tags']);
  10.         if ($this->request->isAjax()) {
  11.             //如果发送的来源是Selectpage,则转发到Selectpage
  12.             if ($this->request->request('keyField')) {
  13.                 return $this->selectpage();
  14.             }
  15.             list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  16.             $total = $this->model
  17.                 ->where($where)
  18.                 ->order($sort, $order)
  19.                 ->count();

  20.             $list = $this->model
  21.                 ->where($where)
  22.                 ->order($sort, $order)
  23.                 ->limit($offset, $limit)
  24.                 ->select();

  25.             $list = collection($list)->toArray();
  26.             $result = array("total" => $total, "rows" => $list);

  27.             return json($result);
  28.         }
  29.         return $this->view->fetch();
  30.     }
  31.     //......
  32. }
复制代码

有关联时的控制器:
  1. class Dash extends Backend
  2. {

  3.     /**
  4.      * Dash模型对象
  5.      * @var \app\admin\model\Dash
  6.      */
  7.     protected $model = null;

  8.     public function _initialize()
  9.     {
  10.         parent::_initialize();
  11.         $this->model = new \app\admin\model\Dash;

  12.     }

  13.     /**
  14.      * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  15.      * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  16.      * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  17.      */

  18.     /**
  19.      * 查看
  20.      */
  21.     public function index()
  22.     {
  23.         //当前是否为关联查询
  24.         $this->relationSearch = true;
  25.         //设置过滤方法
  26.         $this->request->filter(['strip_tags']);
  27.         if ($this->request->isAjax())
  28.         {
  29.             //如果发送的来源是Selectpage,则转发到Selectpage
  30.             if ($this->request->request('keyField'))
  31.             {
  32.                 return $this->selectpage();
  33.             }
  34.             list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  35.             $total = $this->model
  36.                     ->with(['category'])
  37.                     ->where($where)
  38.                     ->order($sort, $order)
  39.                     ->fetchSql(false)
  40.                     ->count();

  41.             $list = $this->model
  42.                     ->with(['category'])
  43.                     ->where($where)
  44.                     ->order($sort, $order)
  45.                     ->limit($offset, $limit)
  46.                     ->fetchSql(false)
  47.                     ->select();
  48.             //file_put_contents(__DIR__.'/../../../runtime/log/sql_'.time().'_log.txt', $total);
  49.             //file_put_contents(__DIR__.'/../../../runtime/log/sql_'.time().'_log1.txt', $list);
  50.             foreach ($list as $row) {

  51.             }
  52.             $list = collection($list)->toArray();
  53.             $result = array("total" => $total, "rows" => $list);

  54.             return json($result);
  55.         }
  56.         return $this->view->fetch();
  57.     }
  58. }
复制代码

无关联时的model

  1. class Demo extends Model
  2. {
  3.     // 表名
  4.     protected $name = 'demo';

  5.     // 自动写入时间戳字段
  6.     protected $autoWriteTimestamp = false;

  7.     // 定义时间戳字段名
  8.     protected $createTime = false;
  9.     protected $updateTime = false;

  10.     // 追加属性
  11.     protected $append = [
  12.     ];
  13. }
复制代码

有关联时的model(多出了一个方法)
  1. class Dash extends Model
  2. {
  3.     // 表名
  4.     protected $name = 'dash';

  5.     // 自动写入时间戳字段
  6.     protected $autoWriteTimestamp = false;

  7.     // 定义时间戳字段名
  8.     protected $createTime = false;
  9.     protected $updateTime = false;

  10.     // 追加属性
  11.     protected $append = [

  12.     ];

  13.     public function category()
  14.     {
  15.         return $this->belongsTo('Category', 'category_id', 'id', [], 'LEFT')->setEagerlyType(0);
  16.     }
  17. }
复制代码

js
  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

  2.     var Controller = {
  3.         index: function () {
  4.             // 初始化表格参数配置
  5.             Table.api.init({
  6.                 extend: {
  7.                     index_url: 'demo/index',
  8.                     add_url: 'demo/add',
  9.                     edit_url: 'demo/edit',
  10.                     del_url: 'demo/del',
  11.                     multi_url: 'demo/multi',
  12.                     table: 'demo',
  13.                 }
  14.             });

  15.             var table = $("#table");

  16.             // 初始化表格
  17.             table.bootstrapTable({
  18.                 url: $.fn.bootstrapTable.defaults.extend.index_url,
  19.                 pk: 'id',
  20.                 sortName: 'id',
  21.                 columns: [
  22.                     [
  23.                         {checkbox: true},
  24.                         {field: 'id', title: __('Id')},
  25.                         {field: 'name', title: __('Name')},
  26.                         {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
  27.                     ]
  28.                 ]
  29.             });

  30.             // 为表格绑定事件
  31.             Table.api.bindevent(table);
  32.         },
  33.         add: function () {
  34.             Controller.api.bindevent();
  35.         },
  36.         edit: function () {
  37.             Controller.api.bindevent();
  38.         },
  39.         api: {
  40.             bindevent: function () {
  41.                 Form.api.bindevent($("form[role=form]"));
  42.             }
  43.         }
  44.     };
  45.     return Controller;
  46. });
复制代码


关联后的js
  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

  2.     var Controller = {
  3.         index: function () {
  4.             // 初始化表格参数配置
  5.             Table.api.init({
  6.                 extend: {
  7.                     index_url: 'dash/index',
  8.                     add_url: 'dash/add',
  9.                     edit_url: 'dash/edit',
  10.                     del_url: 'dash/del',
  11.                     multi_url: 'dash/multi',
  12.                     table: 'dash',
  13.                 }
  14.             });

  15.             var table = $("#table");

  16.             // 初始化表格
  17.             table.bootstrapTable({
  18.                 url: $.fn.bootstrapTable.defaults.extend.index_url,
  19.                 pk: 'id',
  20.                 sortName: 'id',
  21.                 columns: [
  22.                     [
  23.                         {checkbox: true},
  24.                         {field: 'id', title: __('Id')},
  25.                         {field: 'name', title: __('Name')},
  26.                         {field: 'category_id', title: __('Category_id')},
  27.                         {field: 'category.id', title: __('Category.id')},
  28.                         {field: 'category.pid', title: __('Category.pid')},
  29.                         {field: 'category.type', title: __('Category.type')},
  30.                         {field: 'category.name', title: __('Category.name')},
  31.                         {field: 'category.nickname', title: __('Category.nickname')},
  32.                         {field: 'category.flag', title: __('Category.flag'), operate:'FIND_IN_SET', formatter: Table.api.formatter.label},
  33.                         {field: 'category.image', title: __('Category.image'), formatter: Table.api.formatter.image},
  34.                         {field: 'category.keywords', title: __('Category.keywords')},
  35.                         {field: 'category.description', title: __('Category.description')},
  36.                         {field: 'category.diyname', title: __('Category.diyname')},
  37.                         {field: 'category.createtime', title: __('Category.createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
  38.                         {field: 'category.updatetime', title: __('Category.updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
  39.                         {field: 'category.weigh', title: __('Category.weigh')},
  40.                         {field: 'category.status', title: __('Category.status'), formatter: Table.api.formatter.status},
  41.                         {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
  42.                     ]
  43.                 ]
  44.             });

  45.             // 为表格绑定事件
  46.             Table.api.bindevent(table);
  47.         },
  48.         add: function () {
  49.             Controller.api.bindevent();
  50.         },
  51.         edit: function () {
  52.             Controller.api.bindevent();
  53.         },
  54.         api: {
  55.             bindevent: function () {
  56.                 Form.api.bindevent($("form[role=form]"));
  57.             }
  58.         }
  59.     };
  60.     return Controller;
  61. });
复制代码




您需要登录后才可以回帖 登录 | 注册
学习中心
站长自定义文字内容,利用碎片时间,随时随地获取优质内容。
Q设计语言 了解更多
Q Design 提供商家设计所需的指导与资源,帮商家快速完成产品设计、降低生产成本。
学习中心
站长自定义文字内容,利用碎片时间,随时随地获取优质内容。
Q设计语言 了解更多
Q Design 提供商家设计所需的指导与资源,帮商家快速完成产品设计、降低生产成本。