改进response类的returnData方法

This commit is contained in:
thinkphp
2015-12-22 15:41:16 +08:00
parent f726fbf6d2
commit 9ed8ca5e17
4 changed files with 22 additions and 20 deletions

View File

@@ -110,10 +110,8 @@ class App
}
// 操作方法执行完成监听
APP_HOOK && Hook::listen('action_end', $data);
// 单元测试模式下返回数据
if (IN_UNIT_TEST) { return $data; }
// 输出数据
Response::returnData($data, Config::get('default_return_type'), Config::get('response_exit'));
return Response::returnData($data, Config::get('default_return_type'), Config::get('response_type'));
} else {
// 操作方法不是Public 抛出异常
throw new \ReflectionException();
@@ -125,10 +123,8 @@ class App
$data = $method->invokeArgs($instance, [$action, '']);
// 操作方法执行完成监听
APP_HOOK && Hook::listen('action_end', $data);
// 单元测试模式下返回数据
if (IN_UNIT_TEST) { return $data; }
// 输出数据
Response::returnData($data, Config::get('default_return_type'), Config::get('response_exit'));
return Response::returnData($data, Config::get('default_return_type'), Config::get('response_type'));
} else {
throw new Exception('method [ ' . (new \ReflectionClass($instance))->getName() . '->' . $action . ' ] not exists ', 10002);
}
@@ -299,7 +295,7 @@ class App
// 路由检测
if (!empty($config['url_route_on'])) {
// 开启路由 则检测路由配置
Route::register(!empty($config['route'])?$config['route']:null);
Route::register(!empty($config['route']) ? $config['route'] : null);
$result = Route::check($path_info, $depr);
if (false === $result) {
// 路由无效

View File

@@ -19,10 +19,10 @@ class Response
* @access protected
* @param mixed $data 要返回的数据
* @param String $type 返回数据格式
* @param bool $exit 是否终止执行
* @param integer $return 是否返回数据 0 echo 1 return 2 exit
* @return void
*/
public static function returnData($data, $type = '', $exit = true)
public static function returnData($data, $type = '', $return = 0)
{
$headers = [
'json' => 'application/json',
@@ -52,12 +52,17 @@ class Response
$data = $handler . '(' . \org\Transform::jsonEncode($data) . ');';
break;
}
//header('Content-Length:' . strlen($data));
if ($exit) {
exit($data);
} else {
echo $data;
switch ($return) {
case 1:
return $data;
case 2:
exit($data);
case 0:
default:
echo $data;
}
}
/**