Describe the bug
Router::getBasePath() auto-detects the base path using $_SERVER['SCRIPT_NAME']:
static::$serverBasePath = implode('/', array_slice(explode('/', $_SERVER['SCRIPT_NAME']), 0, -1)) . '/';
When using the PHP built-in server (php -S), $_SERVER['SCRIPT_NAME'] does not
reliably resolve to /. Instead it computes a non-root base path (e.g. /api/v1/users/)
and silently strips that prefix from every incoming request URI before route matching.
This causes all routes with path prefixes (e.g. /api/v1/users/login) to return 404,
while only the stripped suffix (e.g. /login) matches — making the bug extremely
difficult to diagnose.
To Reproduce
- Create a Leaf MVC project
- Define routes with prefixes in
app/routes/_app.php:
app()->post('/api/v1/users/register', 'AuthController@register');
app()->get('/api/v1/users/login', 'AuthController@login');
- Serve using PHP built-in server:
php -S localhost:5500 -t public public/index.php
- Visit
http://localhost:5500/api/v1/users/login
Expected behavior
http://localhost:5500/api/v1/users/login matches the defined route and returns 200.
Actual behavior
http://localhost:5500/api/v1/users/login returns 404. Only
http://localhost:5500/login matches.
Workaround
Manually force the base path at the top of app/routes/_app.php:
Environment
- leafs/leaf version: v4.4
- leafs/mvc-core version: v4.8
- PHP SAPI: cli-server (PHP built-in server)
- OS: Linux
Suggested Fix
Default $serverBasePath to '/' instead of auto-detecting from
$_SERVER['SCRIPT_NAME'], or validate the detected value before applying it.
Describe the bug
Router::getBasePath()auto-detects the base path using$_SERVER['SCRIPT_NAME']:When using the PHP built-in server (
php -S),$_SERVER['SCRIPT_NAME']does notreliably resolve to
/. Instead it computes a non-root base path (e.g./api/v1/users/)and silently strips that prefix from every incoming request URI before route matching.
This causes all routes with path prefixes (e.g.
/api/v1/users/login) to return 404,while only the stripped suffix (e.g.
/login) matches — making the bug extremelydifficult to diagnose.
To Reproduce
app/routes/_app.php:php -S localhost:5500 -t public public/index.phphttp://localhost:5500/api/v1/users/loginExpected behavior
http://localhost:5500/api/v1/users/loginmatches the defined route and returns 200.Actual behavior
http://localhost:5500/api/v1/users/loginreturns 404. Onlyhttp://localhost:5500/loginmatches.Workaround
Manually force the base path at the top of
app/routes/_app.php:Environment
Suggested Fix
Default
$serverBasePathto'/'instead of auto-detecting from$_SERVER['SCRIPT_NAME'], or validate the detected value before applying it.