39 lines
947 B
PHP
39 lines
947 B
PHP
<?php
|
|
/**
|
|
* Router for PHP's built-in server (php -S does not read .htaccess).
|
|
*
|
|
* Usage from project root:
|
|
* php -S 127.0.0.1:8080 dev-router.php
|
|
*
|
|
* Then open http://127.0.0.1:8080/website/ for the SPA and /api/... for the API.
|
|
*/
|
|
declare(strict_types=1);
|
|
|
|
$uri = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH) ?: '/';
|
|
|
|
// Existing files (PHP, static assets, etc.)
|
|
$path = __DIR__ . $uri;
|
|
if ($uri !== '/' && is_file($path)) {
|
|
return false;
|
|
}
|
|
|
|
// API → front controller
|
|
if (str_starts_with($uri, '/api')) {
|
|
$_SERVER['SCRIPT_NAME'] = '/api/index.php';
|
|
$_SERVER['SCRIPT_FILENAME'] = __DIR__ . '/api/index.php';
|
|
require __DIR__ . '/api/index.php';
|
|
return true;
|
|
}
|
|
|
|
// Optional: serve SPA for /
|
|
if ($uri === '/') {
|
|
$idx = __DIR__ . '/website/index.html';
|
|
if (is_file($idx)) {
|
|
header('Content-Type: text/html; charset=UTF-8');
|
|
readfile($idx);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|