Utilise le chemin de base plus tard dans le routeur

main
Clément FRÉVILLE 2 years ago
parent cd27eea76e
commit 45eabf2503

@ -27,9 +27,6 @@ class Router
public function setBasePath(string $basePath) public function setBasePath(string $basePath)
{ {
if (PathHelper::startsWith($this->url, $basePath)) {
$this->url = trim(substr($this->url, strlen($basePath)), '/');
}
$this->basePath = $basePath; $this->basePath = $basePath;
} }
@ -67,11 +64,17 @@ class Router
if (!isset($this->routes[$_SERVER['REQUEST_METHOD']])) { if (!isset($this->routes[$_SERVER['REQUEST_METHOD']])) {
throw new RouteNotFoundException('Unknown HTTP method'); throw new RouteNotFoundException('Unknown HTTP method');
} }
if ($this->basePath === '' || PathHelper::startsWith($this->url, $this->basePath)) { $url = $this->url;
foreach ($this->routes[$_SERVER['REQUEST_METHOD']] as $route) { if ($this->basePath !== '') {
if ($route->matches($this->url)) { if (PathHelper::startsWith($url, $this->basePath)) {
return $route->call($di); $url = trim(substr($url, strlen($this->basePath)), '/');
} } else {
throw new RouteNotFoundException('No matching routes');
}
}
foreach ($this->routes[$_SERVER['REQUEST_METHOD']] as $route) {
if ($route->matches($url)) {
return $route->call($di);
} }
} }
throw new RouteNotFoundException('No matching routes'); throw new RouteNotFoundException('No matching routes');

Loading…
Cancel
Save