/** * 获取配置参数不区分大小写 * * @param string $key 支持.获取多维数组 * @param string $default 不存在的时候默认值 * * @return mixed */ public static function get($key = null, $default = null) { // 无参数时获取所有 if (empty($key)) { return self::$_content; } $key = strtolower($key); return Alpha::doteToArr($key, self::$_content['normal'], $default); }
/** * 运行对应的控制器 * * @return void */ public final function runAppController() { //检测csrf跨站攻击 Secure::checkCsrf(Config::get('check_csrf')); // 关闭GPC过滤 防止数据的正确性受到影响 在db层防注入 if (get_magic_quotes_gpc()) { Secure::stripslashes($_GET); Secure::stripslashes($_POST); Secure::stripslashes($_COOKIE); Secure::stripslashes($_REQUEST); //在程序中对get post cookie的改变不影响 request的值 } //session保存方式自定义 if (Config::get('session_user')) { Session::init(); } else { ini_get('session.auto_start') || session_start(); //自动开启session } header('Cache-control: ' . Config::get('http_cache_control')); // 页面缓存控制 //如果有子类中有init()方法 执行Init() eg:做权限控制 if (method_exists($this, "init")) { $this->init(); } //根据动作去找对应的方法 $method = Route::$urlParams['action']; if (method_exists($this, $method)) { $this->{$method}(); } elseif ($GLOBALS['debug']) { Alpha::montFor404Page(); throwException(Lang::get('_ACTION_NOT_FOUND_', Route::$urlParams['action'])); } else { Alpha::montFor404Page(); Response::show404Page(); } }
/** * 匹配路由 * * @param string $pathinfo * * @return mixed */ private static function isRoute(&$pathinfo) { empty($pathinfo) && ($pathinfo[0] = '/'); //网站根地址 $issuccess = array(); $route = self::$rules; switch ($_SERVER['REQUEST_METHOD']) { case 'GET': $rmethod = self::REQUEST_METHOD_GET; break; case 'POST': $rmethod = self::REQUEST_METHOD_POST; break; case 'PUT': $rmethod = self::REQUEST_METHOD_PUT; break; case 'PATCH': $rmethod = self::REQUEST_METHOD_PATCH; break; case 'DELETE': $rmethod = self::REQUEST_METHOD_DELETE; break; case 'OPTIONS': $rmethod = self::REQUEST_METHOD_OPTIONS; break; default: $rmethod = self::REQUEST_METHOD_ANY; } foreach ($route as $k => $v) { $rulesmethod = substr($k, 0, 1); if ($rulesmethod != $rmethod && $rulesmethod != self::REQUEST_METHOD_ANY && $rulesmethod != self::RESTROUTE) { //此条路由不符合当前请求方式 continue; } unset($v); $singleRule = substr($k, 1); $arr = $singleRule === '/' ? array(0 => $singleRule) : explode('/', ltrim($singleRule, '/')); if ($arr[0] == $pathinfo[0]) { array_shift($arr); foreach ($arr as $key => $val) { if (isset($pathinfo[$key + 1]) && $pathinfo[$key + 1] !== '') { if (strpos($val, '\\d') && !is_numeric($pathinfo[$key + 1])) { //数字变量 $route[$k] = false; //匹配失败 break 1; } elseif (strpos($val, ':') === false && $val != $pathinfo[$key + 1]) { //字符串 $route[$k] = false; //匹配失败 break 1; } } else { $route[$k] = false; //匹配失败 break 1; } } } else { $route[$k] = false; //匹配失败 } if ($route[$k] !== false) { //匹配成功的路由 $issuccess[] = $k; } } if (empty($issuccess)) { $returnArr[0] = false; } else { //匹配到多条路由时 选择最长的一条(匹配更精确) usort($issuccess, function ($item1, $item2) { return strlen($item1) >= strlen($item2) ? 0 : 1; }); if (is_callable($route[$issuccess[0]])) { call_user_func($route[$issuccess[0]]); Alpha::Stop(); } $route[$issuccess[0]] = trim($route[$issuccess[0]], '/'); //判断路由的正确性 count(explode('/', $route[$issuccess[0]])) >= 2 || throwException(Lang::get('_ROUTE_PARAM_ERROR_', substr($issuccess[0], 1))); $returnArr[0] = true; $successRoute = explode('/', $issuccess[0]); foreach ($successRoute as $key => $val) { $t = explode('\\d', $val); if (strpos($t[0], ':') !== false) { $_GET[ltrim($t[0], ':')] = $pathinfo[$key]; } unset($pathinfo[$key]); } if (substr($issuccess[0], 0, 1) == self::RESTROUTE) { $actions = explode('/', $route[$issuccess[0]]); $arrKey = count($actions) - 1; $actions[$arrKey] = strtolower($_SERVER['REQUEST_METHOD']) . ucfirst($actions[$arrKey]); $route[$issuccess[0]] = implode('/', $actions); } $returnArr['route'] = $route[$issuccess[0]]; } return $returnArr; }