public static function execute() { if ($uri = trim(router::$uri, '/')) { router::$arguments = explode('/', $uri); router::$module = array_shift(router::$arguments); router::$controller = array_shift(router::$arguments); router::$action = array_shift(router::$arguments); } else { //当$uri 为空,则尝试Query_string模式 router::$arguments = $_GET; router::$module = arr::take('module', router::$arguments); router::$controller = arr::take('controller', router::$arguments); router::$action = arr::take('action', router::$arguments); } }
/** * 解析URI * * URI 由模块名/控制器/动作/参数组成,采用如下的格式: * * @code php * module/controller/action/param1/vlaue1/param2/value2 * @endcode * */ public static function execute() { if ($uri = trim(router::$uri, '/')) { router::$arguments = explode('/', $uri); //分配module、controller、action router::$module = array_shift(router::$arguments); router::$controller = array_shift(router::$arguments); router::$action = array_shift(router::$arguments); //处理参数 $arguments = array(); for ($i = 0, $cnt = count(router::$arguments); $i < $cnt; $i++) { $arguments[$i] = rawurldecode(router::$arguments[$i]); } router::$arguments = $arguments; //unset($_GET['zotop']); //$_GET = array_merge($_GET, array('module'=>router::$module,'controller'=>router::$controller,'action'=>router::$action), $arguments); } else { //当$uri 为空,则尝试Query_string模式 router::$arguments = $_GET; router::$module = arr::take('module', router::$arguments); router::$controller = arr::take('controller', router::$arguments); router::$action = arr::take('action', router::$arguments); } }
/** * 返回当前URL路由的控制器名称,如果未能获取路由分发的控制器,则获取当前应用的默认路由 * * @return string */ public static function controller() { $controller = router::controller(); if (empty($controller)) { $controller = application::$controller; } return $controller; }
/** * 获取当前的控制器的相关信息 * * @return string */ public static function controller($type = 'name') { switch ($type) { case 'name': case 'filename': $return = empty(self::$controller) ? 'index' : self::$controller; break; case 'path': case 'filepath': $return = module::setting(Router::module(), 'root') . DS . router::application() . DS . router::controller('filename') . '.php'; break; case 'class': case 'classname': $return = empty(self::$controller) ? 'IndexController' : ucfirst(self::$controller) . 'Controller'; break; } return $return; }
public static function controllerPath() { $controller = router::controller(); if ($controller) { return zotop::module(router::module(), 'root') . DS . router::application() . DS . router::controller() . '.php'; } return ''; }
/** * 返回当前URL路由的控制器名称,如果未能获取路由分发的控制器,则获取当前应用的默认路由 * * @return string */ public static function getController() { $controller = router::controller(); if (empty($controller)) { $controller = zotop::application(APP_NAME, 'controller'); } return empty($controller) ? 'index' : $controller; }
/** * 渲染视图同make */ function render($name, $par = []) { if (static::$par) { $par = $par + static::$par; } $name = str_replace('.', '/', $name); if (substr($name, 0, 1) == '/') { $this->view_dir = $this->view_dir; $this->theme_dir = $this->theme_dir; $name = substr($name, 1); } $this->__ex($name); $m = router::controller()['module']; unset($n2); if ($m) { if (strpos($name, '/') !== false) { $name = substr($name, strpos($name, '/') + 1); } $n2 = BASE . '/' . static::$modulePath . '/' . $m . '/views/' . $name . '.php'; } $this->block['content'] = $this->find([$this->theme_file, $this->view_file, $n2]); if (!static::$keep_dir) { static::$keep_dir = $this->block['content']; } ob_start(); extract($par, EXTR_OVERWRITE); include $this->block['content']; if (file_exists($this->block['layout'])) { include $this->block['layout']; } $data = trim(ob_get_contents()); ob_end_clean(); if (true === static::$minify) { $data = preg_replace(array('/ {2,}/', '/<!--.*?-->|\\t|(?:\\r?\\n[\\t]*)+/s'), array(' ', ''), $data); } if (static::$cache !== false && static::$cache >= 0) { $url = static::cacheHtml(); file_put_contents($url, $data); } return $data; }
static function callcontroller($controller, $vars, $right = false) { list($folder, $controller, $function) = explode('/', $controller); router::$controller = $controller; if (!class_exists($controller)) { include _BUNDLES_ . '/' . $folder . '/controllers/' . $controller . 'Controller.php'; } $class = new $controller(); $class->loadkernel(); if ($right !== false && !isset($class->session->datas->rights->{$right})) { if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { echo json_encode(array('success' => false, 'message' => 'Vous ne disposez pas des droits necessaires')); exit; } else { router::redirect('login'); } } router::$views_path = _BUNDLES_ . '/' . $folder . '/views'; $class->set('_VIEW_', router::$views_path); $class->set('_URL_', _URL_); call_user_func_array(array($class, $function . 'Action'), $vars); }
function url_array() { return router::controller(); }