diff --git a/src/Router.php b/src/Router.php index 655c83b..bbbb6ae 100644 --- a/src/Router.php +++ b/src/Router.php @@ -43,8 +43,8 @@ class Router */ public function lookup(string $method, string $uri): array { - // normalize uri to be tolerant of trailing slashes - $uri = '/' . trim($uri, '/'); + // trim once and reuse + $trimmed = trim($uri, '/'); // node is a reference to our current location in the node tree $node = $this->routes; @@ -52,8 +52,8 @@ class Router // init the response array $res = ['code' => 0, 'handler' => null, 'params' => []]; - // if the URI is just a slash, we can return the handler for the root node - if ($uri === '/') { + // if the URI is root + if ($trimmed === '') { if (!$this->checkForHandlers($node)) { $res['code'] = 404; return $res; @@ -70,7 +70,7 @@ class Router } // we'll split up the URI into segments and traverse the node tree - foreach (explode('/', trim($uri, '/')) as $segment) { + foreach (explode('/', $trimmed) as $segment) { // check that the next segment is an array (not a callable) and exists, then move to it if (isset($node[$segment]) && is_array($node[$segment])) { $node = $node[$segment]; @@ -182,4 +182,4 @@ class Router { return $this->add('HEAD', $route, $handler); } -} +} \ No newline at end of file