new system prototype
This commit is contained in:
38
dev-router.php
Normal file
38
dev-router.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?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;
|
||||
Reference in New Issue
Block a user