routes; foreach ($segments as $segment) { if ($segment === '') continue; $node = &$node[$segment]; } // Add the handler to the last node $node[$method] = $handler; return $this; } public function lookup(string $method, string $uri): int|array { $node = $this->routes; if (isset($node[$uri])) { if (isset($node[$uri][$method])) return [$node[$uri][$method], []]; return 405; } $uriSegments = explode('/', trim($uri, '/')); $params = []; foreach ($uriSegments as $segment) { if (isset($node[$segment])) { $node = $node[$segment]; continue; } if (isset($node[':x'])) { $params[] = $segment; $node = $node[':x']; continue; } return 404; } if (!isset($node[$method])) return 405; return [$node[$method], $params]; } public function clear(): Router { $this->routes = []; return $this; } }