Gère davantage de cas dans le routeur

main
Clément FRÉVILLE 2 years ago
parent 9bc53b958f
commit 86827d1983

@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace Silex\Router;
final class PathHelper
{
public static function removeEverythingAfter(string $str, string $char): string
{
$pos = strpos($str, $char);
if ($pos !== false) {
return substr($str, 0, $pos);
}
return $str;
}
}

@ -19,16 +19,19 @@ class Router
public function __construct(string $url)
{
$this->url = trim($url, '/');;
$url = PathHelper::removeEverythingAfter($url, '?');
$url = PathHelper::removeEverythingAfter($url, '#');
$this->url = trim($url, '/');
}
public function setBasePath(string $basePath)
{
$pos = strpos($this->url, $basePath);
if ($pos !== false) {
$this->url = substr($this->url, $pos + strlen($basePath));
if ($pos === false) {
$this->url = null;
} else {
$this->url = trim(substr($this->url, $pos + strlen($basePath)), '/');
}
var_dump($this->url);
}
public function get(string $path, callable $callable): self
@ -48,11 +51,13 @@ class Router
if (!isset($this->routes[$_SERVER['REQUEST_METHOD']])) {
throw new RouteNotFoundException('Unknown HTTP method');
}
if ($this->url !== null) {
foreach ($this->routes[$_SERVER['REQUEST_METHOD']] as $route) {
if ($route->matches($this->url)) {
return $route->call($di);
}
}
}
throw new RouteNotFoundException('No matching routes');
}
}

Loading…
Cancel
Save