better basePath support

This commit is contained in:
Maurits van der Schee 2019-07-18 17:42:37 +02:00
commit 31845e1a6a
2 changed files with 22 additions and 1 deletions

View file

@ -7858,6 +7858,7 @@ $config = new Config([
'username' => 'php-crud-api',
'password' => 'php-crud-api',
'database' => 'php-crud-api',
'debug' => false,
]);
$request = RequestFactory::fromGlobals();
$api = new Api($config);

View file

@ -25,7 +25,7 @@ class SimpleRouter implements Router
public function __construct(string $basePath, Responder $responder, Cache $cache, int $ttl, bool $debug)
{
$this->basePath = $basePath;
$this->basePath = $this->detectBasePath($basePath);
$this->responder = $responder;
$this->cache = $cache;
$this->ttl = $ttl;
@ -36,6 +36,21 @@ class SimpleRouter implements Router
$this->middlewares = array();
}
private function detectBasePath(string $basePath): string
{
if ($basePath) {
return $basePath;
}
if (isset($_SERVER['PATH_INFO'])) {
$fullPath = array_shift(explode('?',$_SERVER['REQUEST_URI']));
$path = $_SERVER['PATH_INFO'];
if (substr($fullPath, -1*strlen($path)) == $path) {
return substr($fullPath, 0, -1*strlen($path));
}
}
return '';
}
private function loadPathTree(): PathTree
{
$data = $this->cache->get('PathTree');
@ -101,6 +116,11 @@ class SimpleRouter implements Router
return $request;
}
public function getBasePath(): string
{
return $this->basePath;
}
public function handle(ServerRequestInterface $request): ResponseInterface
{
$request = $this->removeBasePath($request);