29 lines
990 B
PHP
29 lines
990 B
PHP
<?php
|
|
/**
|
|
* PHP内置服务器路由器
|
|
* 用法: php -S 0.0.0.0:8080 router.php
|
|
*/
|
|
|
|
$uri = urldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
|
|
|
|
// 静态文件直接返回
|
|
if ($uri !== '/' && file_exists(__DIR__ . '/public' . $uri)) {
|
|
$filePath = __DIR__ . '/public' . $uri;
|
|
$ext = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
|
|
$mimeTypes = [
|
|
'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg',
|
|
'png' => 'image/png', 'gif' => 'image/gif',
|
|
'webp' => 'image/webp', 'svg' => 'image/svg+xml',
|
|
'css' => 'text/css', 'js' => 'application/javascript',
|
|
'json' => 'application/json', 'ico' => 'image/x-icon',
|
|
];
|
|
header('Content-Type: ' . ($mimeTypes[$ext] ?? 'application/octet-stream'));
|
|
header('Content-Length: ' . filesize($filePath));
|
|
header('Cache-Control: public, max-age=31536000');
|
|
readfile($filePath);
|
|
exit;
|
|
}
|
|
|
|
// 所有请求统一交由 index.php 处理
|
|
require_once __DIR__ . '/public/index.php';
|