diff --git a/src/Splashsky/Router.php b/src/Splashsky/Router.php index f77b83c..7ebfe93 100644 --- a/src/Splashsky/Router.php +++ b/src/Splashsky/Router.php @@ -27,7 +27,7 @@ class Router } self::$routes[] = [ - 'route' => $route, + 'route' => self::trimRoute($route), 'action' => $action, 'methods' => $methods ]; @@ -102,6 +102,12 @@ class Router 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. * @@ -182,12 +188,7 @@ class Router $basePath = rtrim($basePath, '/'); $method = $_SERVER['REQUEST_METHOD']; $uri = parse_url($_SERVER['REQUEST_URI'])['path']; - $path = urldecode(rtrim($uri, '/')); - - // If the path is empty (no slash in URI) place one to satisfy the root route ('/') - if (empty($path)) { - $path = '/'; - } + $path = urldecode(self::trimRoute($uri)); $pathMatchFound = false; $routeMatchFound = false;