/** * 获取Router实例 * @return object Router实例 */ public static function getInstance() { if (empty(self::$instance)) { self::$instance = new self(); } return self::$instance; }
static function init() { if (!isset(self::$obj)) { self::$obj = new self(); } return self::$obj; }
static function run() { $routes = explode('/', $_SERVER['REQUEST_URI']); if ($control_name = filter_var($routes[1], FILTER_VALIDATE_REGEXP, array("options" => array("regexp" => "/^([a-zA-Z0-9])+\$/")))) { self::$control = 'control_' . $control_name; } else { $control_name = 'index'; self::$control = 'control_index'; } if ($model_name = filter_var($routes[2], FILTER_VALIDATE_REGEXP, array("options" => array("regexp" => "/^([a-zA-Z0-9])+\$/")))) { $model_name = $control_name . '_' . $model_name; } else { $model_name = $control_name . '_index'; } $view_name = $model_name . '.view'; $control_path = "engine/controls/" . strtolower(self::$control) . ".php"; if (is_readable($control_path)) { include_once $control_path; settype($msg, "string"); } else { include_once "engine/controls/control_exception.php"; self::$control = "control_Exception"; $model_name = "exception"; $view_name = "exception.view"; $msg = "Route to controller not found"; } $controller = new self::$control(); $controller->model_name = $model_name; $controller->view_name = $view_name; $controller->initmsg = $msg; if (method_exists($controller, 'init')) { $controller->init(); } }
public static function init() { if (null !== self::$_instance) { return self::$_instance; } self::$_instance = new self(); return self::$_instance; }
/** * Init the router and returns it * * @return Router */ public static function getInstance() { if (self::$_instance === null) { self::$_instance = new self(); //get the routes if (is_file(CONFIG_DIR . self::$routeFile) && empty(self::$routes)) { self::$routes = (include CONFIG_DIR . self::$routeFile); //sort by lenght of route url uasort(self::$routes, function ($a, $b) { return strlen($b['url']) - strlen($a['url']); }); } } return self::$_instance; }
public static function initialize() { self::getInstance(); $controller = APP_PATH . DS . 'controllers' . DS . strtolower(self::replace('controller')) . '.php'; if (file_exists($controller)) { include_once $controller; $controller = '\\controller\\' . ucfirst(self::replace('controller')); if (class_exists($controller, false)) { if (method_exists($controller, self::replace('method'))) { if (sizeof(self::$params) == 0) { call_user_func(array(new $controller(), self::replace('method'))); } else { call_user_func_array(array(new $controller(), self::replace('method')), self::$params); } } else { if (self::$method != DEFAULT_METHOD) { $params = array(); if (!in_array(self::$method, self::$params)) { $params[] = self::$method; } self::$method = DEFAULT_METHOD; self::$params = array_merge($params, self::$params); return self::initialize(); } } } } else { if (self::$controller != DEFAULT_CONTROLLER) { $params = array(); if (self::$controller != DEFAULT_CONTROLLER) { $params[] = self::$controller; } if (self::$method != DEFAULT_METHOD) { $params[] = self::$method; } $method = $params[0]; if (self::$controller == $params[0]) { array_shift($params); } self::$controller = DEFAULT_CONTROLLER; self::$method = $method; self::$params = array_merge($params, self::$params); return self::initialize(); } } }
/** * Показывать или нет статистику выполение скрипта * Иногда бывает необходимо отключить показ, например, при выводе RSS ленты * * @param bool $bState */ public static function SetIsShowStats($bState) { self::$bShowStats = $bState; }
/** * 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; // 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::$error_callback) { self::$error_callback = function () { header($_SERVER['SERVER_PROTOCOL'] . " 404 Not Found"); echo '404'; }; } if (!is_object(self::$error_callback)) { //call object controller and method self::invokeObject(self::$error_callback, null, 'No routes found.'); if (self::$halts) { return; } } else { call_user_func(self::$error_callback); if (self::$halts) { return; } } } }