/** * Dispatch * @return bool */ public function dispatch() { // Detect the current URI. $uri = Url::detectUri(); // First, we will supose that URI is associated with an Asset File. if (Request::isGet() && $this->dispatchFile($uri)) { return true; } // Not an Asset File URI? Routes the current request. $method = Request::getMethod(); // Search the defined Routes for matches; invoke the associated Callback, if any. foreach ($this->routes as $route) { if ($route->match($uri, $method, false)) { // Found a valid Route; process it. $this->matchedRoute = $route; $callback = $route->callback(); if (is_object($callback)) { // Invoke the Route's Callback with the associated parameters. call_user_func_array($callback, $route->params()); return true; } // Pattern based Route. $regex = $route->regex(); // Prepare the URI used by autoDispatch, applying the REGEX if exists. if (!empty($regex)) { $uri = preg_replace('#^' . $regex . '$#', $callback, $uri); } else { $uri = $callback; } break; } } // Auto-dispatch the processed URI; quit if the attempt finished successfully. if ($this->autoDispatch($uri)) { return true; } // The dispatching failed; invoke the Error Callback with the current URI as parameter. $params = array(htmlspecialchars($uri, ENT_COMPAT, 'ISO-8859-1', true)); $this->invokeObject($this->callback(), $params); return false; }
/** * Dispatch route * @return bool */ public function dispatch() { // Detect the current URI. $uri = Url::detectUri(); // First, we will supose that URI is associated with an Asset File. if (Request::isGet() && $this->dispatchFile($uri)) { return true; } // Not an Asset File URI? Routes the current request. $method = Request::getMethod(); // If there exists a Catch-All Route, firstly we add it to Routes list. if ($this->defaultRoute !== null) { array_push($this->routes, $this->defaultRoute); } foreach ($this->routes as $route) { if ($route->match($uri, $method)) { // Found a valid Route; process it. $this->matchedRoute = $route; $callback = $route->callback(); if ($callback !== null) { // Invoke the Route's Callback with the associated parameters. return $this->invokeObject($callback, $route->params()); } return true; } } // No valid Route found; invoke the Error Callback with the current URI as parameter. $params = array(htmlspecialchars($uri, ENT_COMPAT, 'ISO-8859-1', true)); $this->invokeObject($this->callback(), $params); return false; }