config = $config; require CORE_PATH.'/func/common.php'; } //运行程序 public function run(){ global $is_script; spl_autoload_register(array($this,'loadClass')); $this->origin(); $this->setReporting(); $this->unregisterGlobals(); $this->setDbConfig(); if(!$is_script){ $this->route(); } } //跨域请求处理 public function origin(){ $allow_origin = $this->config['origin'];//跨域访问的时候才会存在此字段 $origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : ''; if (in_array($origin, $allow_origin)) { header('Access-Control-Allow-Origin:' . $origin); header("Access-Control-Allow-Methods: *"); header('Access-Control-Allow-Headers:*'); header("Access-Control-Allow-Credentials: true"); } else { return; } } //路由处理 public function route(){ $controllerName = $this->config['defaultController']; $actionName = $this->config['defaultAction']; $param = array(); $url = $_SERVER['REQUEST_URI']; //清除?之后的内容 $position = strpos($url,'?'); $url = $position === false?$url:substr($url,0,$position); //删除前后的/ $url = trim($url,'/'); if($url){ //使用 / 分割 $urlArray = explode('/',$url); //删除空的数组元素 $urlArray = array_filter($urlArray); //获取控制器名 $controllerName = array_shift($urlArray).'\\controller\\'.ucfirst(array_shift($urlArray)); //获取动作名 $actionName = $urlArray?array_shift($urlArray):$actionName; //获取url参数 $param = $urlArray?$urlArray:array(); } //判断控制器和操作是否存在 $controller = 'app\\'.$controllerName; if(!class_exists($controller)){ exit($controller.'控制器不存在!'); } if(!method_exists($controller,$actionName)){ exit($actionName.'方法不存在!'); } //实例化控制器,这个后续结合controller基类一起看 $dispatch = new $controller($controllerName,$actionName); //以下等同于$dispatch->$actionName($param); call_user_func_array(array($dispatch,$actionName),$param); } //检测开发环境 public function setReporting(){ if(APP_DEBUG === true){ error_reporting(E_ALL); ini_set('display_errors','On'); } else { error_reporting(E_ALL); ini_set('display_errors','Off'); ini_set('log_errors','On'); } } //删除敏感字符 public function stripSlashesDeep($value){ $value = is_array($value)?array_map(array($this,'stripSlashesDeep'),$value):stripSlashes($value); return $value; } //检测自定义全局变量并移除 public function unregisterGlobals(){ if(ini_get('register_globals')){ $array = array('_SESSION','_POST','_GET','_COOKIE','_REQUEST','_SERVER','_ENV','_FILES'); foreach ($array as $value){ foreach ($GLOBALS[$value] as $key=>$var){ if($var===$GLOBALS[$key]){ unset($GLOBALS[$key]); } } } } } //配置数据库信息 public function setDbConfig(){ if(isset($this->config['db'])){ define('DB_HOST',$this->config['db']['host']); define('DB_PORT',$this->config['db']['port']); define('DB_NAME',$this->config['db']['dbname']); define('DB_USER',$this->config['db']['username']); define('DB_PASS',$this->config['db']['password']); } } //自动加载类 public function loadClass($className){ $vendor = substr($className,0,strpos($className,'\\')); $vendorDIR = $this->config['namespace'][$vendor]; $filePath = substr($className,strlen($vendor)).'.php'; //包含应用文件 $file = strtr($vendorDIR.$filePath,'\\',DIRECTORY_SEPARATOR); if(!is_file($file)){ return; } include $file; } }