🧼 Clean up routing behavior with new trim method

This commit is contained in:
Splashsky 2021-07-16 07:14:33 -05:00
parent 1393e0face
commit d0bef18c59

View File

@ -27,7 +27,7 @@ class Router
} }
self::$routes[] = [ self::$routes[] = [
'route' => $route, 'route' => self::trimRoute($route),
'action' => $action, 'action' => $action,
'methods' => $methods 'methods' => $methods
]; ];
@ -102,6 +102,12 @@ class Router
self::$defaultConstraint = $constraint; self::$defaultConstraint = $constraint;
} }
private static function trimRoute(string $route): string
{
$route = trim(trim($route), '/');
return "/$route";
}
/** /**
* Accepts a callable that defines routes, and adds a prefix to them. * Accepts a callable that defines routes, and adds a prefix to them.
* *
@ -182,12 +188,7 @@ class Router
$basePath = rtrim($basePath, '/'); $basePath = rtrim($basePath, '/');
$method = $_SERVER['REQUEST_METHOD']; $method = $_SERVER['REQUEST_METHOD'];
$uri = parse_url($_SERVER['REQUEST_URI'])['path']; $uri = parse_url($_SERVER['REQUEST_URI'])['path'];
$path = urldecode(rtrim($uri, '/')); $path = urldecode(self::trimRoute($uri));
// If the path is empty (no slash in URI) place one to satisfy the root route ('/')
if (empty($path)) {
$path = '/';
}
$pathMatchFound = false; $pathMatchFound = false;
$routeMatchFound = false; $routeMatchFound = false;