mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-01 12:42:48 +08:00
改进response类的returnData方法
This commit is contained in:
9
base.php
9
base.php
@@ -39,6 +39,7 @@ defined('APP_HOOK') or define('APP_HOOK', false); // 是否开启HOOK
|
||||
defined('ENV_PREFIX') or define('ENV_PREFIX', 'T_'); // 环境变量的配置前缀
|
||||
defined('IS_API') or define('IS_API', false); // 是否API接口
|
||||
defined('IN_UNIT_TEST') or define('IN_UNIT_TEST', false); // 是否为单元测试
|
||||
defined('APP_AUTO_BUILD') or define('APP_AUTO_BUILD', false); // 是否自动生成应用模块
|
||||
|
||||
// 应用模式 默认为普通模式
|
||||
defined('APP_MODE') or define('APP_MODE', function_exists('saeAutoLoader') ? 'sae' : 'common');
|
||||
@@ -311,11 +312,11 @@ function S($name, $value = '', $options = null)
|
||||
* @param string $level 日志级别
|
||||
* @return void|array
|
||||
*/
|
||||
function trace($log='[think]', $level = 'log')
|
||||
function trace($log = '[think]', $level = 'log')
|
||||
{
|
||||
if('[think]'==$log){
|
||||
if ('[think]' == $log) {
|
||||
return \think\Log::getLog();
|
||||
}else{
|
||||
\think\Log::record($log,$level);
|
||||
} else {
|
||||
\think\Log::record($log, $level);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,8 +13,8 @@ return [
|
||||
'default_return_type' => 'html',
|
||||
// 默认语言
|
||||
'default_lang' => 'zh-cn',
|
||||
// response输出终止执行
|
||||
'response_exit' => true,
|
||||
// response输出方式 0 echo 1 return 2 exit
|
||||
'response_type' => 0,
|
||||
// 默认AJAX 数据返回格式,可选JSON XML ...
|
||||
'default_ajax_return' => 'JSON',
|
||||
// 默认JSONP格式返回的处理方法
|
||||
|
||||
@@ -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) {
|
||||
// 路由无效
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user