/** * Compares the two path, and only proceed when requirements are met. * * @param Route $route The route curerntly being checked. */ private static function checkUrlMatch(Route $route) { $path = $route->path(); $method = $route->method(); $url = Url::get()->{$method}(); $urlLength = count($url); $pathLength = count($path); // We only want to proceed, if we have exactly as many parts // in the parsed URL, as the path has. If it is not the case, // then we have nothing to do with this route anymore. if ($urlLength !== $pathLength) { return; } /** * The variable that contains all the variables to be passed * to the loader. * * @var array */ $params = []; // Cycle through all the elements, while checking for variables, // the user defined in the route. If there is such a variable, // we add it to the parameters, which we will pass to the loader. for ($i = 0; $i < $urlLength; $i++) { // If the given parts are not matching, then we need to check // if the path is waiting a variable, or not. If not, then this // path is not the path the browser requested. if ($url[$i] !== $path[$i]) { // If the route's part is not a variable, our work is done // here, since this route can't be the one we are looking for. if ($path[$i][0] !== '$') { return; } // If the route's path's part is starting with a '$', then // we are dealing with a variable. So we just add it to the // array that contains it all. $varname = substr($path[$i], 1, strlen($path[$i]) - 1); $params[$varname] = $url[$i]; } } $result = Kernel::load('\\App\\Controllers\\' . $route->calls(), $params); // If we get an array as result, we assume it is meant to be a JSON // object, so we just parse it: if (is_array($result)) { echo json_encode($result); } else { echo $result; } self::$loadedOnce = true; }