/** * 开始自动路由 * * @param string $dir 目录,默认为 action 表示应用目录下的action目录 * @param string $path 路径,默认使用 $_GET['__path__'],如为空则为 / */ public static function run($dir = 'action', $path = '_____NORMAL_____') { // 目录不能以/开头及结尾 if (substr($dir, 0, 1) == '/') { $dir = substr($dir, 1); } if (substr($dir, -1) == '/') { $dir = substr($dir, strlen($dir) - 1); } // 路径必须以/开头,但不能以/结尾 if ($path == '_____NORMAL_____') { $path = @$_GET['__path__']; } if (empty($path)) { $path = '/'; } if (substr($path, 0, 1) != '/') { $path = '/' . $path; } if ($path != '/' && substr($path, -1) == '/') { $path = substr($path, strlen($path) - 1); } // 中间件处理 ROUTER::runMiddleware($path); $filename = APP_ROOT . $dir . $path . (substr($path, -1) == '/' ? '/index' : '') . '.php'; DEBUG::put("path={$path}, file={$filename}", 'Router'); if (file_exists($filename)) { require $filename; } else { ROUTER::notFound($path, $filename); } }