Skip to content

getBasePath() auto-detection incorrectly strips URI segments when using PHP built-in server #323

Description

@lelandyang

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

  1. Create a Leaf MVC project
  2. 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');
  1. Serve using PHP built-in server: php -S localhost:5500 -t public public/index.php
  2. 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:

app()->setBasePath('/');

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions