Przeglądaj źródła

feat: Apache 部署适配(子目录 base path 自动检测)

caesar 1 miesiąc temu
rodzic
commit
b1517c1df9
2 zmienionych plików z 17 dodań i 3 usunięć
  1. 1 1
      app/config/env.dev.php
  2. 16 2
      public/index.php

+ 1 - 1
app/config/env.dev.php

@@ -10,7 +10,7 @@ return [
         'timezone' => 'Asia/Shanghai',
         'locale' => 'zh-CN',
         'name' => 'Glacier',
-        'base_url' => 'http://127.0.0.1:8099',
+        'base_url' => 'http://127.0.0.1/glacier', // Apache 符号链接挂载路径
     ],
     'db' => [
         'host' => '127.0.0.1',

+ 16 - 2
public/index.php

@@ -37,8 +37,22 @@ Middleware::register('auth', [new AuthMiddleware(), 'handle']);
 Middleware::register('permission', [new PermissionMiddleware(), 'handle']);
 
 try {
-    // 解析请求路径(去 query,规范化尾部斜杠)
-    $path = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH) ?: '/';
+    // 解析请求路径(去 query)
+    $requestUri = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH) ?: '/';
+
+    // 部署前缀自动检测:支持子目录部署(如 http://host/glacier/xxx)。
+    // 根部署(DocumentRoot 直指 public)时前缀为空,行为不变。
+    $scriptName = str_replace('\\', '/', $_SERVER['SCRIPT_NAME'] ?? '/index.php');
+    $basePath = rtrim(str_replace('\\', '/', dirname($scriptName)), '/');
+    if ($basePath === '.' || $basePath === '/') {
+        $basePath = '';
+    }
+    if ($basePath !== '' && str_starts_with($requestUri, $basePath)) {
+        $requestUri = substr($requestUri, strlen($basePath)) ?: '/';
+    }
+
+    // 规范化尾部斜杠
+    $path = $requestUri;
     if (strlen($path) > 1) {
         $path = rtrim($path, '/');
     }