Gère davantage de cas dans le routeur
This commit is contained in:
17
src/Silex/Router/PathHelper.php
Normal file
17
src/Silex/Router/PathHelper.php
Normal file
@@ -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,9 +51,11 @@ class Router
|
||||
if (!isset($this->routes[$_SERVER['REQUEST_METHOD']])) {
|
||||
throw new RouteNotFoundException('Unknown HTTP method');
|
||||
}
|
||||
foreach ($this->routes[$_SERVER['REQUEST_METHOD']] as $route) {
|
||||
if ($route->matches($this->url)) {
|
||||
return $route->call($di);
|
||||
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');
|
||||
|
Reference in New Issue
Block a user