fix: strip base path in API router for subdirectory deployments
The API router's parse_path() only stripped /api from the URI, but when deployed in a subdirectory (e.g. /protocol-droid/), the URI is /protocol-droid/api/auth/register — the /api regex didn't match. Now detects base path from SCRIPT_NAME and strips it before routing.
This commit is contained in:
@@ -57,6 +57,18 @@ function respond(int $code, array $data): void {
|
|||||||
function parse_path(): array {
|
function parse_path(): array {
|
||||||
$uri = $_SERVER['REQUEST_URI'] ?? '/';
|
$uri = $_SERVER['REQUEST_URI'] ?? '/';
|
||||||
$path = parse_url($uri, PHP_URL_PATH);
|
$path = parse_url($uri, PHP_URL_PATH);
|
||||||
|
|
||||||
|
// Detect and strip the base path (for subdirectory deployments)
|
||||||
|
$scriptName = $_SERVER['SCRIPT_NAME'] ?? '/index.php';
|
||||||
|
// SCRIPT_NAME for /protocol-droid/api/index.php is /protocol-droid/api/index.php
|
||||||
|
// Go up two levels to get the base path: dirname(dirname(...)) = /protocol-droid
|
||||||
|
$basePath = rtrim(str_replace('\\', '/', dirname(dirname($scriptName))), '/');
|
||||||
|
if ($basePath === '/' || $basePath === '.') $basePath = '';
|
||||||
|
if ($basePath && strpos($path, $basePath) === 0) {
|
||||||
|
$path = substr($path, strlen($basePath));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Strip /api prefix
|
||||||
$path = preg_replace('#^/api#', '', $path);
|
$path = preg_replace('#^/api#', '', $path);
|
||||||
$path = trim($path, '/');
|
$path = trim($path, '/');
|
||||||
return $path === '' ? [] : explode('/', $path);
|
return $path === '' ? [] : explode('/', $path);
|
||||||
|
|||||||
Reference in New Issue
Block a user