Test.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * @author: liaofei<136327134@qq.com>
  4. * @day: 2020/9/12
  5. */
  6. namespace app\controller\admin;
  7. use crmeb\basic\BaseController;
  8. use think\helper\Str;
  9. class Test extends BaseController
  10. {
  11. public function index()
  12. {
  13. }
  14. public function rule()
  15. {
  16. $this->app = app();
  17. $rule = request()->get('rule', 'storeapi');
  18. $this->app->route->setTestMode(true);
  19. $this->app->route->clear();
  20. $path = $this->app->getRootPath() . 'route' . DIRECTORY_SEPARATOR;
  21. $files = is_dir($path) ? scandir($path) : [];
  22. foreach ($files as $file) {
  23. if (strpos($file, '.php')) {
  24. include $path . $file;
  25. }
  26. }
  27. $ruleList = $this->app->route->getRuleList();
  28. $ruleNewList = [];
  29. foreach ($ruleList as $item) {
  30. if (Str::contains($item['rule'], $rule)) {
  31. $ruleNewList[] = $item;
  32. }
  33. }
  34. foreach ($ruleNewList as $key => &$value) {
  35. $only = $value['option']['only'] ?? [];
  36. $route = is_string($value['route']) ? explode('/', $value['route']) : [];
  37. $value['route'] = is_string($value['route']) ? $value['route'] : '';
  38. $action = $route[count($route) - 1] ?? null;
  39. if ($only && $action && !in_array($action, $only)) {
  40. unset($ruleNewList[$key]);
  41. }
  42. $except = $value['option']['except'] ?? [];
  43. if ($except && $action && in_array($action, $except)) {
  44. unset($ruleNewList[$key]);
  45. }
  46. }
  47. echo "<html lang=\"zh-CN\">
  48. <head>
  49. <title>路由地址列表</title>
  50. </head>
  51. <link rel='stylesheet' type='text/css' href='https://www.layuicdn.com/layui/css/layui.css' />
  52. <body>
  53. <div style='margin: 20px'>
  54. <fieldset class=\"layui-elem-field layui-field-title\" style=\"margin-top: 20px;\">
  55. <legend>路由地址列表</legend>
  56. </fieldset>
  57. <div class=\"layui-form\">
  58. <table class=\"layui-table\">
  59. <thead>
  60. <tr>
  61. <th>请求方式</th>
  62. <th>接口地址</th>
  63. <th>接口名称</th>
  64. <th>接口方法</th>
  65. </tr>
  66. </thead>
  67. <tbody>
  68. ";
  69. $allAction = ['delete', 'index', 'update', 'edit', 'save', 'create', 'read'];
  70. foreach ($ruleNewList as $route) {
  71. $option = $route['option']['real_name'] ?? null;
  72. if (is_array($option)) {
  73. foreach ($allAction as $action) {
  74. if (Str::contains($route['route'], $action)) {
  75. $real_name = $option[$action] ?? '';
  76. }
  77. }
  78. } else {
  79. $real_name = $option;
  80. }
  81. $rule = $route['rule'];
  82. echo "<tr>
  83. <td>$route[method]</td>
  84. <td>" . htmlspecialchars($rule) . "</td>
  85. <td>$real_name</td>
  86. <td>$route[route]</td>
  87. </tr>";
  88. }
  89. echo "</table></div></div>";
  90. }
  91. }