/** * 验证请求是否合法 * 1. 如果请求参数m,c,a都为空,则走默认的 */ private function checkRequest() { $InitPHP_conf = InitPHP::getConfig(); $controller = isset($_GET['c']) ? $_GET['c'] : ''; $action = isset($_GET['a']) ? $_GET['a'] : ''; if ($InitPHP_conf['ismodule'] == true) { $module = isset($_GET['m']) ? $_GET['m'] : ''; if ($module == "" && $controller == "" && $action == "") { $module = $_GET['m'] = $this->default_module; $controller = $_GET['c'] = $this->default_controller; $action = $_GET['a'] = $this->default_action; } //如果module不在白名单中,则直接返回404 if (!in_array($module, $this->module_list) || empty($module)) { return InitPHP::return404(); } $module = $module . '/'; } else { if ($controller == "" && $action == "") { $controller = $_GET['c'] = $this->default_controller; $action = $_GET['a'] = $this->default_action; } $module = ''; } //controller处理,如果导入Controller文件失败,则返回404 $path = rtrim($InitPHP_conf['controller']['path'], '/') . '/'; $controllerClass = $controller . $this->controller_postfix; $controllerFilePath = $path . $module . $controllerClass . '.php'; if (!InitPHP::import($controllerFilePath)) { //如果开启了首字母大写,例如请求c=my,则类名为MyController $controllerClass = ucfirst($controller) . $this->controller_postfix; //改成大写 $controllerFilePath = $path . $module . $controllerClass . '.php'; if (!InitPHP::import($controllerFilePath)) { return InitPHP::return404(); } } $controllerObj = InitPHP::loadclass($controllerClass); //处理Action,如果方法不存在,则直接返回404 list($whiteList, $methodList) = $this->parseWhiteList($controllerObj->initphp_list); if ($action != $this->default_action) { if (!in_array($action, $whiteList)) { return InitPHP::return404(); //如果Action不在白名单中 } else { if ($methodList[$action]) { $method = strtolower($_SERVER['REQUEST_METHOD']); if (!in_array($method, $methodList[$action])) { //检查提交的HTTP METHOD return InitPHP::return405(); //如果请求Method不正确,则返回405 } } } } return $controllerObj; }