Skip to content

Commit bbfc7bd

Browse files
committed
FIX: virtualUri computation.
NEW: appBaseUri attribute exposes the base URL of a sub-application (ex: ‘admin’).
1 parent 26dc6fb commit bbfc7bd

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

subsystems/web-server/WebServer/WebServer.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,22 +72,25 @@ function run ()
7272
function setup ()
7373
{
7474
/** @var ServerRequestInterface $request */
75-
$request = ServerRequest::fromGlobals ();
76-
$uri = $request->getUri ();
77-
$scriptName = $request->getServerParams () ['SCRIPT_NAME'];
78-
$realBasePath = dirname ($scriptName);
79-
$basePath = dirnameEx ($scriptName, $this->kernelSettings->urlDepth + 1);
80-
$scheme = $uri->getScheme () ?: 'http';
81-
$port = $uri->getPort ();
82-
$baseUrl = sprintf ('%s://%s%s%s', $scheme, $uri->getHost (),
75+
$request = ServerRequest::fromGlobals ();
76+
$uri = $request->getUri ();
77+
$scriptName = $request->getServerParams () ['SCRIPT_NAME'];
78+
$appBasePath = dirname ($scriptName);
79+
$basePath = dirnameEx ($scriptName, $this->kernelSettings->urlDepth + 1);
80+
$scheme = $uri->getScheme () ?: 'http';
81+
$port = $uri->getPort ();
82+
$baseUrl = sprintf ('%s://%s%s%s', $scheme, $uri->getHost (),
8383
$port ? ($port == 80 && $scheme == 'http' || $port == 443 && $scheme == 'https' ? '' : ":$port") : '',
8484
$basePath);
85-
$path = $uri->getPath ();
86-
$virtualUri = ltrim (substr ($path, strlen ($basePath)), '/');
87-
$realVirtualUri = substr ($path, strlen ($realBasePath));
85+
$path = $uri->getPath ();
86+
$virtualUri = ltrim (substr ($path, strlen ($basePath)), '/');
87+
$appVirtualUri = substr ($path, strlen ($appBasePath));
8888
// Strip trailing slash from virtual URI if it matches a real URI (ex: for sub-applications that have their own index.php on a subfolder)
89-
if (substr ($realVirtualUri, -1) == '/')
89+
if ($appVirtualUri == '/')
9090
$virtualUri = rtrim ($virtualUri, '/');
91+
$appBaseUri = substr ($appBasePath, strlen ($basePath) + 1);
92+
if ($appBaseUri === false)
93+
$appBaseUri = '';
9194
$query = $uri->getQuery ();
9295

9396
$this->kernelSettings->baseUrl = $baseUrl;
@@ -98,6 +101,7 @@ function setup ()
98101
$request = $request->withAttribute ('originalUri', "$baseUrl/$virtualUri" . ($query ? "?$query" : ''));
99102
$request = $request->withAttribute ('baseUri', $basePath);
100103
$request = $request->withAttribute ('baseUrl', $baseUrl);
104+
$request = $request->withAttribute ('appBaseUri', $appBaseUri);
101105
$this->request = $request->withAttribute ('virtualUri', $virtualUri);
102106
}
103107

0 commit comments

Comments
 (0)