// +---------------------------------------------------------------------- namespace app\controller\api; use think\facade\View; use think\facade\Cache; use think\event\RouteLoaded; use crmeb\basic\BaseController; class Route extends BaseController { public $type = ['admin','merchant','api','openapi','manager','pc','service']; public $type_name = [ 'admin' => '平台后台', 'merchant' => '商户后台', 'api' => '用户端', 'openapi' => '开放接口', 'manager' => '员工端', 'pc' => 'PC端', 'service' => '客服端' ]; public function list() { return app('json')->success('请开发者注释当前行代码,才可查看路由列表'); $type = $this->request->param('type',''); $type = $type == '' ? $this->type : [$type]; foreach ($type as $route) { $data[$route] = $this->getRoute($route); } $table = ''; foreach ($data as $t => $list) { foreach ($list as $item) { $table .= ''; $table .= " {$this->type_name[$t]} "; foreach ($item as $value) { $table .= "" . htmlspecialchars($value, ENT_QUOTES, 'UTF-8') . ""; } $table .= ''; } } $html = $this->getHtml($table); return view::display($html); } protected function getRoute($type) { $cache_key = $type.'_route_list'; $res = Cache::get($cache_key); if ($res) return $res; $this->app->route->clear(); $path = root_path(). 'route' . DIRECTORY_SEPARATOR . $type . DIRECTORY_SEPARATOR; $file = root_path(). 'route' . DIRECTORY_SEPARATOR . $type . '.php'; if (is_file($file)) { include $file; } if (is_dir($path)) { $dir = scandir($path); foreach ($dir as $file) { if ($file == '.' || $file == '..') { continue; } include $path.$file; } } $this->app->event->trigger(RouteLoaded::class); $res = $this->routeList(); Cache::set($cache_key, $res, 60); return $res; } protected function reloadRoute() { $this->app->route->clear(); $path = root_path(). 'route' . DIRECTORY_SEPARATOR; $dir = scandir($path); foreach ($dir as $file) { if ($file == '.' || $file == '..') { continue; } if (is_file($path.$file)) { include $path.$file; } } $this->app->event->trigger(RouteLoaded::class); } protected function routeList() { $routeList = $this->app->route->getRuleList(); $rows = []; foreach ($routeList as $item) { $item['route'] = $item['route'] instanceof \Closure ? '' : $item['route']; $row = [ $item['rule'], ($item['option']['prefix'] ?? '').$item['route'], $item['method'], $item['name'], $item['option']['_alias'] ?? '' ]; $rows[] = $row; } $this->reloadRoute(); return $rows; } protected function getHtml($content) { $html = << 路由列表 $content
所属端路由方法位置请求方式名称方法注释
HTML; return $html; } }