root[$method]; $segments = explode('/', trim($route, '/')); foreach ($segments as $segment) { if (!isset($node[$segment])) { $node[$segment] = ['_children' => [], '_handler' => null]; } $node = &$node[$segment]['_children']; } $node['_handler'] = $handler; return $this; } // Find and handle the route public function lookup(string $method, string $uri): int|array { $node = &$this->root[$method]; $segments = explode('/', trim($uri, '/')); $params = []; foreach ($segments as $segment) { if (isset($node[$segment])) { $node = &$node[$segment]['_children']; } else { // Handle dynamic parameters (e.g., :id) $dynamicSegment = $this->matchDynamicSegment($node, $segment); if ($dynamicSegment) { $params[] = $segment; $node = &$node[$dynamicSegment]['_children']; } else { return 404; } } } // Check if a handler exists for the current node if (isset($node['_handler'])) return [$node['_handler'], $params]; return 404; } // Match dynamic route segments like ':id' private function matchDynamicSegment(array $node, string $segment) { foreach ($node as $key => $value) { if (strpos($key, ':') === 0) return $key; } return null; } public function clear(): Router { $this->root = []; return $this; } }