diff --git a/framework/base/router.class.php b/framework/base/router.class.php index cb3ce79495..2ab8a7fe24 100644 --- a/framework/base/router.class.php +++ b/framework/base/router.class.php @@ -12,6 +12,7 @@ * May you find forgiveness for yourself and forgive others. * May you share freely, never taking more than you give. */ +include dirname(__DIR__) . '/exception/EndResponseException.class.php'; class baseRouter { /** @@ -1831,88 +1832,100 @@ class baseRouter */ public function loadModule() { - $appName = $this->appName; - $moduleName = $this->moduleName; - $methodName = $this->methodName; + try { + $appName = $this->appName; + $moduleName = $this->moduleName; + $methodName = $this->methodName; - /* - * 引入该模块的control文件。 - * Include the control file of the module. - **/ - $file2Included = $this->setActionExtFile() ? $this->extActionFile : $this->controlFile; - chdir(dirname($file2Included)); - helper::import($file2Included); + /* + * 引入该模块的control文件。 + * Include the control file of the module. + **/ + $file2Included = $this->setActionExtFile() ? $this->extActionFile : $this->controlFile; + chdir(dirname($file2Included)); + helper::import($file2Included); - /* - * 设置control的类名。 - * Set the class name of the control. - **/ - $className = class_exists("my$moduleName") ? "my$moduleName" : $moduleName; - if(!class_exists($className)) $this->triggerError("the control $className not found", __FILE__, __LINE__, $exit = true); + /* + * 设置control的类名。 + * Set the class name of the control. + **/ + $className = class_exists("my$moduleName") ? "my$moduleName" : $moduleName; + if(!class_exists($className)) $this->triggerError("the control $className not found", __FILE__, __LINE__, $exit = true); - /* - * 创建control类的实例。 - * Create a instance of the control. - **/ - $module = new $className(); - if(!method_exists($module, $methodName)) $this->triggerError("the module $moduleName has no $methodName method", __FILE__, __LINE__, $exit = true); - $this->control = $module; + /* + * 创建control类的实例。 + * Create a instance of the control. + **/ + $module = new $className(); + if(!method_exists($module, $methodName)) $this->triggerError("the module $moduleName has no $methodName method", __FILE__, __LINE__, $exit = true); + $this->control = $module; - /* include default value for module*/ - $defaultValueFiles = glob($this->getTmpRoot() . "defaultvalue/*.php"); - if($defaultValueFiles) foreach($defaultValueFiles as $file) include $file; + /* include default value for module*/ + $defaultValueFiles = glob($this->getTmpRoot() . "defaultvalue/*.php"); + if($defaultValueFiles) foreach($defaultValueFiles as $file) include $file; - /* - * 使用反射机制获取函数参数的默认值。 - * Get the default settings of the method to be called using the reflecting. - * - * */ - $defaultParams = array(); - $methodReflect = new reflectionMethod($className, $methodName); - foreach($methodReflect->getParameters() as $param) - { - $name = $param->getName(); - - $default = '_NOT_SET'; - if(isset($paramDefaultValue[$appName][$className][$methodName][$name])) + /* + * 使用反射机制获取函数参数的默认值。 + * Get the default settings of the method to be called using the reflecting. + * + * */ + $defaultParams = array(); + $methodReflect = new reflectionMethod($className, $methodName); + foreach($methodReflect->getParameters() as $param) { - $default = $paramDefaultValue[$appName][$className][$methodName][$name]; - } - elseif(isset($paramDefaultValue[$className][$methodName][$name])) - { - $default = $paramDefaultValue[$className][$methodName][$name]; - } - elseif($param->isDefaultValueAvailable()) - { - $default = $param->getDefaultValue(); + $name = $param->getName(); + + $default = '_NOT_SET'; + if(isset($paramDefaultValue[$appName][$className][$methodName][$name])) + { + $default = $paramDefaultValue[$appName][$className][$methodName][$name]; + } + elseif(isset($paramDefaultValue[$className][$methodName][$name])) + { + $default = $paramDefaultValue[$className][$methodName][$name]; + } + elseif($param->isDefaultValueAvailable()) + { + $default = $param->getDefaultValue(); + } + + $defaultParams[$name] = $default; } - $defaultParams[$name] = $default; + /** + * 根据PATH_INFO或者GET方式设置请求的参数。 + * Set params according PATH_INFO or GET. + */ + if($this->config->requestType != 'GET') + { + $this->setParamsByPathInfo($defaultParams); + } + else + { + $this->setParamsByGET($defaultParams); + } + + if($this->config->framework->filterParam == 2) + { + $_GET = validater::filterParam($_GET, 'get'); + $_COOKIE = validater::filterParam($_COOKIE, 'cookie'); + } + + /* 调用该方法 Call the method. */ + call_user_func_array(array($module, $methodName), $this->params); + $this->checkAPIFile(); + return $module; + } catch (EndResponseException $endResponseException) { + echo $endResponseException->getContent(); } - - /** - * 根据PATH_INFO或者GET方式设置请求的参数。 - * Set params according PATH_INFO or GET. - */ - if($this->config->requestType != 'GET') + if (isset($module)) { - $this->setParamsByPathInfo($defaultParams); + return $module; } else { - $this->setParamsByGET($defaultParams); + return false; } - - if($this->config->framework->filterParam == 2) - { - $_GET = validater::filterParam($_GET, 'get'); - $_COOKIE = validater::filterParam($_COOKIE, 'cookie'); - } - - /* 调用该方法 Call the method. */ - call_user_func_array(array($module, $methodName), $this->params); - $this->checkAPIFile(); - return $module; } /** diff --git a/framework/exception/EndResponseException.class.php b/framework/exception/EndResponseException.class.php new file mode 100644 index 0000000000..6380f5519f --- /dev/null +++ b/framework/exception/EndResponseException.class.php @@ -0,0 +1,45 @@ +content = $content; + return $exception; + } + + /** + * Get 响应内容 + * + * @return string + */ + public function getContent() + { + return $this->content; + } +} diff --git a/framework/helper.class.php b/framework/helper.class.php index 7a169ccaa1..b69232705e 100644 --- a/framework/helper.class.php +++ b/framework/helper.class.php @@ -248,6 +248,17 @@ class helper extends baseHelper if(empty($jsonDecode)) return $response; return $jsonDecode; } + + /** + * 代替 die、exit 函数终止并输出 + * + * @param string $content + * @return void + */ + public static function die($content) + { + throw EndResponseException::create($content); + } } /** diff --git a/module/my/control.php b/module/my/control.php index ccd55eaa9b..8d037b644b 100644 --- a/module/my/control.php +++ b/module/my/control.php @@ -816,14 +816,19 @@ class my extends control */ public function editProfile() { - if($this->app->user->account == 'guest') die(js::alert('guest') . js::locate('back')); + if($this->app->user->account == 'guest') + { + echo js::alert('guest'), js::locate('back'); + return; + } if(!empty($_POST)) { $_POST['account'] = $this->app->user->account; $_POST['groups'] = $this->dao->select('`group`')->from(TABLE_USERGROUP)->where('account')->eq($this->post->account)->fetchPairs('group', 'group'); $this->user->update($this->app->user->id); - if(dao::isError()) die(js::error(dao::getError())); - die(js::locate($this->createLink('my', 'profile'), 'parent')); + if(dao::isError()) helper::die(js::error(dao::getError())); + echo js::locate($this->createLink('my', 'profile'), 'parent'); + return; } $this->app->loadConfig('user');