Files
2026-05-07 12:46:32 +08:00

41 lines
1.1 KiB
PHP

<?php
/**
* PHP 内置开发服务器路由文件
*
* 用法:php -S localhost:8080 -t public/ public/router.php
*
* Nginx 部署不需要此文件,Nginx 有自己的 URL 重写规则。
*/
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
// API 路由 — 所有 /api/ 请求统一转发到 api.php
if (str_starts_with($uri, '/api/')) {
require __DIR__ . '/api.php';
return true;
}
// 静态资源(CSS/JS/图片/字体/上传文件等)
if ($uri !== '/' && preg_match('/\.(css|js|png|jpg|jpeg|gif|ico|svg|woff2?|ttf|eot|webp|webm|mp4|pdf|zip)$/i', $uri)) {
$file = __DIR__ . $uri;
if (is_file($file)) {
return false; // 让 PHP 内置服务器直接返回静态文件
}
}
// 根路径
if ($uri === '/' || $uri === '') {
require __DIR__ . '/index.php';
return true;
}
// 已存在的 PHP 文件直接访问
$phpFile = __DIR__ . $uri;
if (is_file($phpFile) && str_ends_with($uri, '.php')) {
return false; // 让 PHP 内置服务器处理
}
// 其他请求回退到 index.php
require __DIR__ . '/index.php';
return true;