From d0bef18c59fb360c30fa1528b4f6f926ad6ef415 Mon Sep 17 00:00:00 2001 From: Splashsky Date: Fri, 16 Jul 2021 07:14:33 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=BC=20Clean=20up=20routing=20behavior?= =?UTF-8?q?=20with=20new=20trim=20method?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Splashsky/Router.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) 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;