public static function dispatch() { $url = new Url(); $uri = $url->getPath(); foreach (array_reverse(self::$routes, true) as $route => $class) { if (preg_match("~^{$route}\$~", $uri, $params)) { Router::$current = $class; $return = call_user_func_array(array('Controller', 'dispatch'), array_merge(array($class), array_slice($params, 1))); if (!(false === $return)) { $vars = get_class_vars($class); $type = 'text/html'; if (isset($vars['type'])) { $type = $vars['type']; } # PHP >= 5.3 # if ( isset($class::$type) ) # $type = $class::$type; //Response::setHeader('Content-Type', 'application/xhtml+xml'); Response::setHeader('Content-Type', "{$type};charset=UTF-8"); Response::setBody($class, $return); return; } Router::$current = null; } } if (Response::getHttpResponseCode() == 200) { $class = 'Error404'; $return = Controller::dispatch($class); Response::setHeader('Content-Type', 'text/html;charset=UTF-8'); Response::setBody($class, $return); //Response::setHeader('HTTP/1.0 404 Not Found'); //Response::setHttpResponseCode(404); //Response::setBody('404', 'Error 404'); } }
public static function dispatch() { $pathInfo = self::getPathInfo(); foreach (self::$_routingTable as $key => $route) { if (preg_match($route['regx'], $pathInfo, $matches)) { self::$current = $key; try { $params = NULL; if (!empty($route['param'])) { unset($matches[0]); $params = array_combine($route['param'], $matches); } $widget = Widget::widget($route['widget'], NULL, $params); if (isset($route['action'])) { $method = $route['action']; $widget->{$method}(); } return; } catch (Exception $e) { if (404 == $e->getCode()) { Widget::destory($route['widget']); continue; } throw $e; } } } /** 载入路由异常支持 */ throw new HandleException("Path '{$pathInfo}' not found", 404); }
/** * Run the framework. * * @static * @return bool */ public static function run() { // Router determines controller, method, and arguments Router::current(); // Load controller if it exists $exists = Loader::get('controller' . DIRECTORY_SEPARATOR . Router::$controller); if ($exists) { return self::runController(Router::$controller); } // We couldn't find a controller. self::error404(); }
/** * Run the framework. */ public static function run() { // Router determines controller, method, and arguments Router::current(); // Load controller if it exists $exists = Loader::get('controller', Router::$controller); if ($exists === TRUE) { $controller = ucfirst(Router::$controller . '_Controller'); return self::run_controller($controller); } // We couldn't load the controller. self::error_404(); }
/** * Detects whether to construct a URL if an array is given, return a defined slug if a string is given, * or construct a URL if an array is given with a slug index. * * @access public * @param string|array $url * - 'slugName' // Returns the defined array for the slug * - array('slug' => 'slugName', 'id' => 5) // Merges with slugName's array and appends the id index * - array('controller' => 'main', 'action' => 'index') // Merges with default values and returns * @return string|array * @static */ public static function detect($url) { if (is_array($url)) { if (isset($url['slug'])) { $slug = $url['slug']; unset($url['slug']); if ($route = self::slug($slug)) { return $url + $route; } } else { return $url + Router::current(); } } else { if ($route = self::slug($url)) { return $route; } } return $url; }