优化Response类,兼容rest控制器

This commit is contained in:
huangdijia
2015-12-23 18:20:49 +08:00
parent 541553f3bc
commit e4a66b73b6
3 changed files with 14 additions and 8 deletions

View File

@@ -111,7 +111,7 @@ class App
// 操作方法执行完成监听 // 操作方法执行完成监听
APP_HOOK && Hook::listen('action_end', $data); APP_HOOK && Hook::listen('action_end', $data);
// 输出数据 // 输出数据
return Response::send($data, Config::get('default_return_type'), Config::get('response_return')); return Response::send($data, Response::type(), Config::get('response_return'));
} else { } else {
// 操作方法不是Public 抛出异常 // 操作方法不是Public 抛出异常
throw new \ReflectionException(); throw new \ReflectionException();
@@ -124,7 +124,7 @@ class App
// 操作方法执行完成监听 // 操作方法执行完成监听
APP_HOOK && Hook::listen('action_end', $data); APP_HOOK && Hook::listen('action_end', $data);
// 输出数据 // 输出数据
return Response::send($data, Config::get('default_return_type'), Config::get('response_return')); return Response::send($data, Response::type(), Config::get('response_return'));
} else { } else {
throw new Exception('method [ ' . (new \ReflectionClass($instance))->getName() . '->' . $action . ' ] not exists ', 10002); throw new Exception('method [ ' . (new \ReflectionClass($instance))->getName() . '->' . $action . ' ] not exists ', 10002);
} }

View File

@@ -92,8 +92,9 @@ class Response
* @param string $type 输出内容的格式类型 * @param string $type 输出内容的格式类型
* @return void * @return void
*/ */
public static function type($type) public static function type($type = null)
{ {
if(is_null($type)) return self::$type;
self::$type = $type; self::$type = $type;
} }
@@ -137,6 +138,7 @@ class Response
'time' => NOW_TIME, 'time' => NOW_TIME,
'data' => $data, 'data' => $data,
]; ];
if($type) self::type($type);
return $result; return $result;
} }
@@ -158,12 +160,14 @@ class Response
'url' => $url ?: $_SERVER["HTTP_REFERER"], 'url' => $url ?: $_SERVER["HTTP_REFERER"],
'wait' => $wait, 'wait' => $wait,
]; ];
$type = Config::get('default_return_type');
if(IS_AJAX){ if(IS_AJAX){
Config::set('default_return_type', Config::get('default_ajax_return')); $type = Config::get('default_ajax_return');
} }
if ('html' == Config::get('default_return_type')) { if ('html' == $type) {
$result = \think\View::getInstance()->fetch(Config::get('dispatch_jump_tmpl'), $result); $result = \think\View::getInstance()->fetch(Config::get('dispatch_jump_tmpl'), $result);
} }
self::type($type);
return $result; return $result;
} }
@@ -185,12 +189,14 @@ class Response
'url' => $url ?: 'javascript:history.back(-1);', 'url' => $url ?: 'javascript:history.back(-1);',
'wait' => $wait, 'wait' => $wait,
]; ];
$type = Config::get('default_return_type');
if(IS_AJAX){ if(IS_AJAX){
Config::set('default_return_type', Config::get('default_ajax_return')); $type = Config::get('default_ajax_return');
} }
if ('html' == Config::get('default_return_type')) { if ('html' == $type) {
$result = \think\View::getInstance()->fetch(Config::get('dispatch_jump_tmpl'), $result); $result = \think\View::getInstance()->fetch(Config::get('dispatch_jump_tmpl'), $result);
} }
self::type($type);
return $result; return $result;
} }

View File

@@ -46,6 +46,6 @@ if (APP_HOOK && isset($mode['tags'])) {
if (APP_AUTO_BUILD && is_file(APP_PATH . 'build.php')) { if (APP_AUTO_BUILD && is_file(APP_PATH . 'build.php')) {
Build::run(include APP_PATH . 'build.php'); Build::run(include APP_PATH . 'build.php');
} }
Loader::addNamespace('tests', TEST_PATH);
// 执行应用 // 执行应用
!IN_UNIT_TEST && App::run(); !IN_UNIT_TEST && App::run();