ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
[TOC] * * * * * ## 1 数据输出文件源代码(thinkphp/library/think/Response.php) ~~~ protected static $tramsform = null; protected static $type = ''; protected static $data = ''; protected static $isExit = false; ~~~ ~~~ public static function send($data = '', $type = '', $return = false) { $type = strtolower($type ?: self::$type); $headers = [ 'json' => 'application/json', 'xml' => 'text/xml', 'html' => 'text/html', 'jsonp' => 'application/javascript', 'script' => 'application/javascript', 'text' => 'text/plain', ]; if (!headers_sent() && isset($headers[$type])) { header('Content-Type:' . $headers[$type] . '; charset=utf-8'); } $data = $data ?: self::$data; if (is_callable(self::$tramsform)) { $data = call_user_func_array(self::$tramsform, [$data]); } else { switch ($type) { case 'json': $data = json_encode($data, JSON_UNESCAPED_UNICODE); break; case 'jsonp': $handler = !empty($_GET[Config::get('var_jsonp_handler')]) ? $_GET[Config::get('var_jsonp_handler')] : Config::get('default_jsonp_handler'); $data = $handler . '(' . json_encode($data, JSON_UNESCAPED_UNICODE) . ');'; break; case '': case 'html': case 'text': break; default: APP_HOOK && Hook::listen('return_data', $data); } } if ($return) { return $data; } echo $data; self::isExit() && exit(); } ~~~ ~~~ public static function tramsform($callback) { self::$tramsform = $callback; } ~~~ ~~~ public static function type($type = null) { if (is_null($type)) { return self::$type ?: Config::get('default_return_type'); } self::$type = $type; } ~~~ ~~~ public static function data($data) { self::$data = $data; } ~~~ ~~~ public static function isExit($exit = null) { if (is_null($exit)) { return self::$isExit; } self::$isExit = (boolean) $exit; } ~~~ ~~~ public static function result($data, $code = 0, $msg = '', $type = '') { $result = [ 'code' => $code, 'msg' => $msg, 'time' => NOW_TIME, 'data' => $data, ]; if ($type) { self::type($type); } return $result; } ~~~ ~~~ public static function success($msg = '', $data = '', $url = null, $wait = 3) { $code = 1; if (is_numeric($msg)) { $code = $msg; $msg = ''; } $result = [ 'code' => $code, 'msg' => $msg, 'data' => $data, 'url' => is_null($url) ? $_SERVER["HTTP_REFERER"] : $url, 'wait' => $wait, ]; $type = IS_AJAX ? Config::get('default_ajax_return') : Config::get('default_return_type'); if ('html' == $type) { $result = \think\View::instance()->fetch(Config::get('dispatch_success_tmpl'), $result); } self::type($type); return $result; } ~~~ ~~~ public static function error($msg = '', $data = '', $url = null, $wait = 3) { $code = 0; if (is_numeric($msg)) { $code = $msg; $msg = ''; } $result = [ 'code' => $code, 'msg' => $msg, 'data' => $data, 'url' => is_null($url) ? 'javascript:history.back(-1);' : $url, 'wait' => $wait, ]; $type = IS_AJAX ? Config::get('default_ajax_return') : Config::get('default_return_type'); if ('html' == $type) { $result = \think\View::instance()->fetch(Config::get('dispatch_error_tmpl'), $result); } self::type($type); return $result; } ~~~ ~~~ public static function redirect($url, $params = []) { $http_response_code = 301; if (is_int($params) && in_array($params, [301, 302])) { $http_response_code = $params; $params = []; } $url = preg_match('/^(https?:|\/)/', $url) ? $url : Url::build($url, $params); header('Location: ' . $url, true, $http_response_code); } ~~~ ~~~ public static function header($name, $value) { header($name . ':' . $value); } ~~~ ## 2 分析 Response.php是框架的数据输出实现文件。 框架在路由解析url,调度运行相关模块控制后,返回结果到App::run(), 之后框架调用Response::send()输出运行结果到客户端。 ~~~ 转换方法,格式类型,数据内容,脚本结束 protected static $tramsform = null; protected static $type = ''; protected static $data = ''; protected static $isExit = false; ~~~ * * * * * >[info] send() 输出字符串到客户端 `public static function send($data = '', $type = '', $return = false){}` > $data: 输出数据内容 > $type: 输出数据格式 json,xml,html,jsonp,script,text等值 > $return: 返回数据不输出到客户端 * * * * * >[info] transform() 自定义数据转换方法 `public static function tramsform($callback){}` > $callback:接受$data为参数,并返回转换结果的可调用过程 * * * * * >[info] type() 设置默认输出类型 `public static function type($type = null){} ` > $type:和上面的send()的$type相同。 * * * * * >[info] data() 设置输出数据 `public static function data($data){}` > $data:待输出数据内容 * * * * * >[info] isExit() 脚本结束控制 `public static function isExit($exit = null)` > $exit:bool值。true:send()输出数据后停止运行;false:不停止运行。 * * * * * >[info] result() 输出api格式的数据 `public static function result($data, $code = 0, $msg = '', $type = '')` > $data:输出数据内容 > $code:状态码 > $msg:提示信息 > $time:输出数据时间 * * * * * >[info] success() error() 操作成功和失败跳转操作 ~~~ public static function success($msg = '', $data = '', $url = null, $wait = 3){} public static function error($msg = '', $data = '', $url = null, $wait = 3){} ~~~ > $msg:提示信息 > $data:返回的数据内容 > $url:待跳转地址 > $wait:待跳转时间 * * * * * >[info] redirect() 重定向跳转 `public static function redirect($url, $params = []){}` > $url:重定向地址 > $params:跳转时携带的参数 * * * * * >[info] header() 设置响应头 `public static function header($name, $value)` > $name:header中的字段名称 > $value:header中的字段对应值 ## 3 总结 Response.php中的大多数方法用来在 控制器controller操作方法action完成后的最后输出数据操作。 transform() type() data() isEixt() header()设置输出状态控制与输出数据内容 send() result() 输出数据到客户端 success() error() 操作成功与失败的跳转操作 redirect() 重定向操作 数据输出的使用方法见 使用范例的 输出控制