/** * Runs the callback for the given request. */ public static function dispatch() { $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); $method = $_SERVER['REQUEST_METHOD']; $searches = array_keys(static::$patterns); $replaces = array_values(static::$patterns); self::$routes = str_replace('//', '/', self::$routes); $found_route = false; // parse query parameters $query = ''; $q_arr = array(); if (strpos($uri, '&') > 0) { $query = substr($uri, strpos($uri, '&') + 1); $uri = substr($uri, 0, strpos($uri, '&')); $q_arr = explode('&', $query); foreach ($q_arr as $q) { $qobj = explode('=', $q); $q_arr[] = array($qobj[0] => $qobj[1]); if (!isset($_GET[$qobj[0]])) { $_GET[$qobj[0]] = $qobj[1]; } } } // check if route is defined without regex if (in_array($uri, self::$routes)) { $route_pos = array_keys(self::$routes, $uri); // foreach route position foreach ($route_pos as $route) { if (self::$methods[$route] == $method || self::$methods[$route] == 'ANY') { $found_route = true; //if route is not an object if (!is_object(self::$callbacks[$route])) { //call object controller and method self::invokeObject(self::$callbacks[$route]); if (self::$halts) { return; } } else { //call closure call_user_func(self::$callbacks[$route]); if (self::$halts) { return; } } } } // end foreach } else { // check if defined with regex $pos = 0; // foreach routes foreach (self::$routes as $route) { $route = str_replace('//', '/', $route); if (strpos($route, ':') !== false) { $route = str_replace($searches, $replaces, $route); } if (preg_match('#^' . $route . '$#', $uri, $matched)) { if (self::$methods[$pos] == $method || self::$methods[$pos] == 'ANY') { $found_route = true; //remove $matched[0] as [1] is the first parameter. array_shift($matched); if (!is_object(self::$callbacks[$pos])) { //call object controller and method self::invokeObject(self::$callbacks[$pos], $matched); if (self::$halts) { return; } } else { //call closure call_user_func_array(self::$callbacks[$pos], $matched); if (self::$halts) { return; } } } } $pos++; } // end foreach } if (self::$fallback) { //call the auto dispatch method $found_route = self::autoDispatch(); } // run the error callback if the route was not found if (!$found_route) { if (!self::$errorCallback) { self::$errorCallback = function () { header("{$_SERVER['SERVER_PROTOCOL']} 404 Not Found"); $data['title'] = '404'; $data['error'] = "Oops! Page not found."; View::renderTemplate('header', $data); View::render('error/404', $data); View::renderTemplate('footer', $data); }; } if (!is_object(self::$errorCallback)) { //call object controller and method self::invokeObject(self::$errorCallback, null, 'No routes found.'); if (self::$halts) { return; } } else { call_user_func(self::$errorCallback); if (self::$halts) { return; } } } }
public static function dispatch() { $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); $method = $_SERVER['REQUEST_METHOD']; $searches = array_keys(static::$patterns); $replaces = array_values(static::$patterns); self::$routes = str_replace('//', '/', self::$routes); $found_route = false; $query = ''; $q_arr = array(); if (strpos($uri, '&') > 0) { $query = substr($uri, strpos($uri, '&') + 1); $uri = substr($uri, 0, strpos($uri, '&')); $q_arr = explode('&', $query); foreach ($q_arr as $q) { $qobj = explode('=', $q); $q_arr[] = array($qobj[0] => $qobj[1]); if (!isset($_GET[$qobj[0]])) { $_GET[$qobj[0]] = $qobj[1]; } } } if (in_array($uri, self::$routes)) { $route_pos = array_keys(self::$routes, $uri); foreach ($route_pos as $route) { if (self::$methods[$route] == $method || self::$methods[$route] == 'ANY') { $found_route = true; if (!is_object(self::$callbacks[$route])) { self::invokeObject(self::$callbacks[$route]); if (self::$halts) { return; } } else { call_user_func(self::$callbacks[$route]); if (self::$halts) { return; } } } } } else { $pos = 0; foreach (self::$routes as $route) { $route = str_replace('//', '/', $route); if (strpos($route, ':') !== false) { $route = str_replace($searches, $replaces, $route); } if (preg_match('#^' . $route . '$#', $uri, $matched)) { if (self::$methods[$pos] == $method || self::$methods[$pos] == 'ANY') { $found_route = true; array_shift($matched); if (!is_object(self::$callbacks[$pos])) { self::invokeObject(self::$callbacks[$pos], $matched); if (self::$halts) { return; } } else { call_user_func_array(self::$callbacks[$pos], $matched); if (self::$halts) { return; } } } } $pos++; } } if (self::$fallback) { $found_route = self::autoDispatch(); } if (!$found_route) { if (!self::$errorCallback) { self::$errorCallback = function () { header("{$_SERVER['SERVER_PROTOCOL']} 404 Not Found"); $data['title'] = '404'; $data['error'] = "Страница не найдена."; View::render('Error/404', $data); }; } if (!is_object(self::$errorCallback)) { self::invokeObject(self::$errorCallback, null, 'Маршруты не найдены.'); if (self::$halts) { return; } } else { call_user_func(self::$errorCallback); if (self::$halts) { return; } } } }