/** * Check that a valid route has been found. */ protected function checkRoute() { if (Router::current() === null) { return $this->error(404); } }
/** * Check if the current request uri matches this route. * * @return array|false Vars to be passed to the callback */ public function match() { // Get the current uri $uri = $_SERVER['REQUEST_URI']; // We only care about the uri path, not query strings etc. $uri = parse_url($uri, PHP_URL_PATH); // Create an empty array to store return vars in $vars = []; // Sanitize multiple slashes $uri = preg_replace('/\\/+/', '/', $uri); // Remove leading and trailing slashes if ($uri !== '/') { $uri = strpos($uri, '/') === 0 ? substr($uri, 1) : $uri; $uri = substr($uri, strlen($uri), 1) === '/' ? substr($uri, 0, -1) : $uri; } $route = $this->route; if ($uri === $route) { return $vars; } $uri = explode('/', $uri); if (count($uri) > count($this->strings)) { return false; } // Loop through each of the exploded uri parts foreach ($this->strings as $pos => $string) { $bit = isset($uri[$pos]) ? $uri[$pos] : ''; if (!isset($this->strings[$pos]) || !isset($this->placeholders[$pos])) { return false; } // Check if the current position is a string, and if the uri matches if (isset($this->strings[$pos]) && $this->strings[$pos] !== '') { if ($bit !== $this->strings[$pos]) { return false; } continue; } // Check if the current position is a var, and if the uri matches if (isset($this->placeholders[$pos]) && $this->placeholders[$pos] !== '') { if (!$this->matchVar($bit, $this->placeholders[$pos])) { return false; } $vars[] = $bit; continue; } } Router::$current = $this; return $vars; }