创建后台控制器

继续以文章分类为例,我们在actionphp/application/example/controller/admin/目录下创建Cate.php文件,注意这里我们就会用到initadmin里很重要的一个特性:动态页面,咱们不需要去写.vue,只要在这里编写PHP代码即可自动解析生页面,内容如下:

创建后台控制器 - 图1

  1. <?php
  2. /**
  3. * +----------------------------------------------------------------------
  4. * | InitAdmin/actionphp [ InitAdmin渐进式模块化通用后台 ]
  5. * +----------------------------------------------------------------------
  6. * | Copyright (c) 2018-2019 http://initadmin.net All rights reserved.
  7. * +----------------------------------------------------------------------
  8. * | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  9. * +----------------------------------------------------------------------
  10. * | Author: jry <ijry@qq.com>
  11. * +----------------------------------------------------------------------
  12. */
  13. namespace app\example\controller\admin;
  14. use think\Db;
  15. use think\Validate;
  16. use think\facade\Request;
  17. use app\core\controller\common\Admin;
  18. use app\core\util\Tree;
  19. /**
  20. * 文章分类
  21. *
  22. * @author jry <ijry@qq.com>
  23. */
  24. class Cate extends Admin
  25. {
  26. private $example_cate;
  27. protected function initialize()
  28. {
  29. parent::initialize();
  30. $this->example_cate = new \app\example\model\Cate();
  31. }
  32. /**
  33. * 分类列表
  34. *
  35. * @return \think\Response
  36. * @author jry <ijry@qq.com>
  37. */
  38. public function lists()
  39. {
  40. //分类列表
  41. $data_list = $this->example_cate->select()->toArray();
  42. $tree = new Tree();
  43. $data_list = $tree->list2tree($data_list);
  44. //构造动态页面数据
  45. $ibuilder_list = new \app\core\util\ibuilder\IbuilderList();
  46. $list_data = $ibuilder_list->init()
  47. ->addTopButton('add', '添加分类', ['api' => '/v1/admin/example/cate/add'])
  48. ->addRightButton('edit', '修改', ['api' => '/v1/admin//example/cate/edit', 'title' => '修改分类信息'])
  49. ->addRightButton('delete', '删除', [
  50. 'api' => '/v1/admin//example/cate/delete',
  51. 'title' => '确认要删除该分类吗?',
  52. 'modal_type' => 'confirm',
  53. 'width' => '600',
  54. 'okText' => '确认删除',
  55. 'cancelText' => '取消操作',
  56. 'content' => '<p>删除后前台将无法访问</p>',
  57. ])
  58. ->addColumn('id' , 'ID', ['width' => '50px'])
  59. ->addColumn('nickname', '昵称', ['width' => '120px'])
  60. ->addColumn('username', '用户名', ['width' => '120px'])
  61. ->addColumn('mobile', '手机号', ['width' => '120px'])
  62. ->addColumn('email', '邮箱', ['width' => '120px'])
  63. ->addColumn('sortnum', '排序', ['width' => '50px'])
  64. ->addColumn('right_button_list', '操作', [
  65. 'minWidth' => '50px',
  66. 'type' => 'template',
  67. 'template' => 'right_button_list'
  68. ])
  69. ->getData();
  70. //返回数据
  71. return json([
  72. 'code' => 200, 'msg' => '成功', 'data' => [
  73. 'data_list' => $data_list,
  74. 'list_data' => $list_data
  75. ]
  76. ]);
  77. }
  78. /**
  79. * 添加
  80. *
  81. * @return \think\Response
  82. * @author jry <ijry@qq.com>
  83. */
  84. public function add()
  85. {
  86. if (request()->isPost()) {
  87. //数据验证
  88. $validate = Validate::make([
  89. 'title' => 'require'
  90. ],
  91. [
  92. 'title.require' => '分类标题必须'
  93. ]);
  94. $data = input('post.');
  95. if (!$validate->check($data)) {
  96. return json(['code' => 200, 'msg' => $validate->getError(), 'data' => []]);
  97. }
  98. //数据构造
  99. $data_db = $data;
  100. if (count($data_db) <= 0 ) {
  101. return json(['code' => 0, 'msg' => '无数据提交', 'data' => []]);
  102. }
  103. $data_db['status'] = 1;
  104. $data_db['create_time'] = time();
  105. //存储数据
  106. $ret = $this->example_cate->save($data_db);
  107. if ($ret) {
  108. return json(['code' => 200, 'msg' => '添加成功', 'data' => []]);
  109. } else {
  110. return json(['code' => 0, 'msg' => '添加失败:' . $this->core_user->getError(), 'data' => []]);
  111. }
  112. } else {
  113. //构造动态页面数据
  114. $ibuilder_form = new \app\core\util\ibuilder\IbuilderForm();
  115. $form_data = $ibuilder_form->init()
  116. ->setFormMethod('post')
  117. ->addFormItem('title', '标题', 'text', '', [
  118. 'placeholder' => '请输入标题',
  119. 'tip' => '分类标题'
  120. ])
  121. ->addFormRule('title', [
  122. ['required' => true, 'message' => '请填写标题', 'trigger' => 'change'],
  123. ])
  124. ->setFormValues()
  125. ->getData();
  126. //返回数据
  127. return json([
  128. 'code' => 200,
  129. 'msg' => '成功',
  130. 'data' => [
  131. 'form_data' => $form_data
  132. ]
  133. ]);
  134. }
  135. }
  136. /**
  137. * 修改
  138. *
  139. * @return \think\Response
  140. * @author jry <ijry@qq.com>
  141. */
  142. public function edit($id)
  143. {
  144. if (request()->isPut()) {
  145. //数据验证
  146. $validate = Validate::make([
  147. 'title' => 'require'
  148. ],
  149. [
  150. 'title.require' => '标题必须'
  151. ]);
  152. $data = input('post.');
  153. if (!$validate->check($data)) {
  154. return json(['code' => 200, 'msg' => $validate->getError(), 'data' => []]);
  155. }
  156. //数据构造
  157. $data_db = $data;
  158. if (count($data_db) <= 0 ) {
  159. return json(['code' => 0, 'msg' => '无数据提交', 'data' => []]);
  160. }
  161. //存储数据
  162. $ret = $this->example_cate->update($data_db, ['id' => $id]);
  163. if ($ret) {
  164. return json(['code' => 200, 'msg' => '修改信息成功', 'data' => []]);
  165. } else {
  166. return json(['code' => 0, 'msg' => '修改信息失败:' . $this->core_user->getError(), 'data' => []]);
  167. }
  168. } else {
  169. //用户信息
  170. $info = $this->core_user
  171. ->where('id', $id)
  172. ->find();
  173. //构造动态页面数据
  174. $ibuilder_form = new \app\core\util\ibuilder\IbuilderForm();
  175. $form_data = $ibuilder_form->init()
  176. ->setFormMethod('put')
  177. ->addFormItem('title', '标题', 'text', $info['title'], [
  178. 'placeholder' => '请输入标题',
  179. 'tip' => '分类标题'
  180. ])
  181. ->addFormRule('title', [
  182. ['required' => true, 'message' => '请填写标题', 'trigger' => 'change'],
  183. ])
  184. ->setFormValues()
  185. ->getData();
  186. //返回数据
  187. return json([
  188. 'code' => 200,
  189. 'msg' => '成功',
  190. 'data' => [
  191. 'form_data' => $form_data
  192. ]
  193. ]);
  194. }
  195. }
  196. /**
  197. * 删除
  198. *
  199. * @return \think\Response
  200. * @author jry <ijry@qq.com>
  201. */
  202. public function delete($id)
  203. {
  204. //删除
  205. $ret = $this->example_cate
  206. ->where(['id' => $id])
  207. ->find()
  208. ->delete();
  209. if ($ret) {
  210. return json(['code' => 200, 'msg' => '删除成功', 'data' => []]);
  211. } else {
  212. return json(['code' => 0, 'msg' => '删除错误:' . $this->core_user->getError(), 'data' => []]);
  213. }
  214. }
  215. }