Skip to content

Commit ddf37c2

Browse files
committed
better basePath support
1 parent 46126ae commit ddf37c2

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

src/Tqdev/PhpCrudApi/Middleware/Router/SimpleRouter.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,16 @@ private function detectBasePath(string $basePath): string
4242
return $basePath;
4343
}
4444
if (isset($_SERVER['PATH_INFO'])) {
45-
$fullPath = array_shift(explode('?',$_SERVER['REQUEST_URI']));
45+
$fullPath = explode('?', $_SERVER['REQUEST_URI'])[0];
4646
$path = $_SERVER['PATH_INFO'];
47-
if (substr($fullPath, -1*strlen($path)) == $path) {
48-
return substr($fullPath, 0, -1*strlen($path));
47+
if (!$path) {
48+
return $_SERVER['PHP_SELF'];
49+
}
50+
if (substr($fullPath, -1 * strlen($path)) == $path) {
51+
return substr($fullPath, 0, -1 * strlen($path));
4952
}
5053
}
51-
return '';
54+
return '/';
5255
}
5356

5457
private function loadPathTree(): PathTree
@@ -102,15 +105,10 @@ private function getRouteNumbers(ServerRequestInterface $request): array
102105

103106
private function removeBasePath(ServerRequestInterface $request): ServerRequestInterface
104107
{
105-
if ($this->basePath) {
106-
$path = $request->getUri()->getPath();
107-
$basePath = rtrim($this->basePath, '/');
108-
if (substr($path, 0, strlen($basePath)) == $basePath) {
109-
$path = substr($path, strlen($basePath));
110-
$request = $request->withUri($request->getUri()->withPath($path));
111-
}
112-
} elseif (isset($_SERVER['PATH_INFO'])) {
113-
$path = $_SERVER['PATH_INFO'];
108+
$path = $request->getUri()->getPath();
109+
$basePath = rtrim($this->basePath, '/');
110+
if (substr($path, 0, strlen($basePath)) == $basePath) {
111+
$path = substr($path, strlen($basePath));
114112
$request = $request->withUri($request->getUri()->withPath($path));
115113
}
116114
return $request;

0 commit comments

Comments
 (0)