RouteController.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-present http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 老猫 <thinkcmf@126.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller;
  12. use app\admin\model\RouteModel;
  13. use cmf\controller\AdminBaseController;
  14. class RouteController extends AdminBaseController
  15. {
  16. /**
  17. * 路由规则列表
  18. * @adminMenu(
  19. * 'name' => 'URL美化',
  20. * 'parent' => 'admin/Setting/default',
  21. * 'display'=> true,
  22. * 'hasView'=> true,
  23. * 'order' => 10000,
  24. * 'icon' => '',
  25. * 'remark' => 'URL规则管理',
  26. * 'param' => ''
  27. * )
  28. */
  29. public function index()
  30. {
  31. global $CMF_GV_routes;
  32. $routeModel = new RouteModel();
  33. $routes = RouteModel::order("list_order asc")->select();
  34. $routeModel->getRoutes(true);
  35. unset($CMF_GV_routes);
  36. $this->assign("routes", $routes);
  37. return $this->fetch();
  38. }
  39. /**
  40. * 添加路由规则
  41. * @adminMenu(
  42. * 'name' => '添加路由规则',
  43. * 'parent' => 'index',
  44. * 'display'=> false,
  45. * 'hasView'=> true,
  46. * 'order' => 10000,
  47. * 'icon' => '',
  48. * 'remark' => '添加路由规则',
  49. * 'param' => ''
  50. * )
  51. */
  52. public function add()
  53. {
  54. return $this->fetch();
  55. }
  56. /**
  57. * 添加路由规则提交
  58. * @adminMenu(
  59. * 'name' => '添加路由规则提交',
  60. * 'parent' => 'index',
  61. * 'display'=> false,
  62. * 'hasView'=> false,
  63. * 'order' => 10000,
  64. * 'icon' => '',
  65. * 'remark' => '添加路由规则提交',
  66. * 'param' => ''
  67. * )
  68. */
  69. public function addPost()
  70. {
  71. if ($this->request->isPost()) {
  72. $data = $this->request->param();
  73. $routeModel = new RouteModel();
  74. $result = $this->validate($data, 'Route');
  75. if ($result !== true) {
  76. $this->error($result);
  77. }
  78. $routeModel->save($data);
  79. $this->success("添加成功!", url("Route/index", ['id' => $routeModel->id]));
  80. }
  81. }
  82. /**
  83. * 路由规则编辑
  84. * @adminMenu(
  85. * 'name' => '路由规则编辑',
  86. * 'parent' => 'index',
  87. * 'display'=> false,
  88. * 'hasView'=> true,
  89. * 'order' => 10000,
  90. * 'icon' => '',
  91. * 'remark' => '路由规则编辑',
  92. * 'param' => ''
  93. * )
  94. */
  95. public function edit()
  96. {
  97. $id = $this->request->param("id", 0, 'intval');
  98. $route = RouteModel::where('id', $id)->find()->toArray();
  99. $this->assign($route);
  100. return $this->fetch();
  101. }
  102. /**
  103. * 路由规则编辑提交
  104. * @adminMenu(
  105. * 'name' => '路由规则编辑提交',
  106. * 'parent' => 'index',
  107. * 'display'=> false,
  108. * 'hasView'=> false,
  109. * 'order' => 10000,
  110. * 'icon' => '',
  111. * 'remark' => '路由规则编辑提交',
  112. * 'param' => ''
  113. * )
  114. */
  115. public function editPost()
  116. {
  117. if ($this->request->isPost()) {
  118. $data = $this->request->param();
  119. $routeModel = new RouteModel();
  120. $result = $this->validate($data, 'Route');
  121. if ($result !== true) {
  122. $this->error($result);
  123. }
  124. $routeModel->where('id', $data['id'])->update($data);
  125. $this->success("保存成功!", url("Route/index"));
  126. }
  127. }
  128. /**
  129. * 路由规则删除
  130. * @adminMenu(
  131. * 'name' => '路由规则删除',
  132. * 'parent' => 'index',
  133. * 'display'=> false,
  134. * 'hasView'=> false,
  135. * 'order' => 10000,
  136. * 'icon' => '',
  137. * 'remark' => '路由规则删除',
  138. * 'param' => ''
  139. * )
  140. */
  141. public function delete()
  142. {
  143. if ($this->request->isPost()) {
  144. $id = $this->request->param('id', 0, 'intval');
  145. RouteModel::destroy($id);
  146. $this->success("删除成功!");
  147. }
  148. }
  149. /**
  150. * 路由规则禁用
  151. * @adminMenu(
  152. * 'name' => '路由规则禁用',
  153. * 'parent' => 'index',
  154. * 'display'=> false,
  155. * 'hasView'=> false,
  156. * 'order' => 10000,
  157. * 'icon' => '',
  158. * 'remark' => '路由规则禁用',
  159. * 'param' => ''
  160. * )
  161. */
  162. public function ban()
  163. {
  164. if ($this->request->isPost()) {
  165. $id = $this->request->param("id", 0, 'intval');
  166. $data = [];
  167. $data['status'] = 0;
  168. $data['id'] = $id;
  169. $routeModel = new RouteModel();
  170. $routeModel->where('id', $data['id'])->update($data);
  171. $this->success("禁用成功!");
  172. }
  173. }
  174. /**
  175. * 路由规则启用
  176. * @adminMenu(
  177. * 'name' => '路由规则启用',
  178. * 'parent' => 'index',
  179. * 'display'=> false,
  180. * 'hasView'=> false,
  181. * 'order' => 10000,
  182. * 'icon' => '',
  183. * 'remark' => '路由规则启用',
  184. * 'param' => ''
  185. * )
  186. */
  187. public function open()
  188. {
  189. if ($this->request->isPost()) {
  190. $id = $this->request->param("id", 0, 'intval');
  191. $data = [];
  192. $data['status'] = 1;
  193. $data['id'] = $id;
  194. $routeModel = new RouteModel();
  195. $routeModel->where('id', $data['id'])->update($data);
  196. $this->success("启用成功!");
  197. }
  198. }
  199. /**
  200. * 路由规则排序
  201. * @adminMenu(
  202. * 'name' => '路由规则排序',
  203. * 'parent' => 'index',
  204. * 'display'=> false,
  205. * 'hasView'=> false,
  206. * 'order' => 10000,
  207. * 'icon' => '',
  208. * 'remark' => '路由规则排序',
  209. * 'param' => ''
  210. * )
  211. */
  212. public function listOrder()
  213. {
  214. $routeModel = new RouteModel();
  215. parent::listOrders($routeModel);
  216. $this->success("排序更新成功!");
  217. }
  218. /**
  219. * 选择 URL
  220. * @adminMenu(
  221. * 'name' => '选择URL',
  222. * 'parent' => 'index',
  223. * 'display'=> false,
  224. * 'hasView'=> true,
  225. * 'order' => 10000,
  226. * 'icon' => '',
  227. * 'remark' => '选择URL',
  228. * 'param' => ''
  229. * )
  230. */
  231. public function select()
  232. {
  233. $routeModel = new RouteModel();
  234. $urls = $routeModel->getAppUrls();
  235. $this->assign('urls', $urls);
  236. return $this->fetch();
  237. }
  238. function _suggest_url($action, $url)
  239. {
  240. $actionArr = explode('/', $action);
  241. $params = array_keys($url['vars']);
  242. $urlDepr1Params = [];
  243. $urlDepr2Params = [];
  244. if (!empty($params)) {
  245. foreach ($params as $param) {
  246. if (empty($url['vars'][$param]['require'])) {
  247. array_push($urlDepr1Params, "[:$param]");
  248. } else {
  249. array_push($urlDepr1Params, ":$param");
  250. }
  251. array_push($urlDepr2Params, htmlspecialchars('<') . $param . htmlspecialchars('>'));
  252. }
  253. }
  254. if ($actionArr[2] == 'index') {
  255. $actionArr[1] = cmf_parse_name($actionArr[1]);
  256. return empty($params) ? $actionArr[1] . '$' : ($actionArr[1] . '/' . implode('/', $urlDepr1Params) /*. '或' . $actionArr[1] . '-' . implode('-', $urlDepr2Params)*/);
  257. } else {
  258. $actionArr[2] = cmf_parse_name($actionArr[2]);
  259. return empty($params) ? $actionArr[2] . '$' : ($actionArr[2] . '/' . implode('/', $urlDepr1Params) /*. '或' . $actionArr[2] . '-' . implode('-', $urlDepr2Params)*/);
  260. }
  261. }
  262. function _url_vars($url)
  263. {
  264. if (!empty($url['vars'])) {
  265. return implode(',', array_keys($url['vars']));
  266. }
  267. return '';
  268. }
  269. }