phpresponse返回,附:(Response.php)数据输出[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,
phpjson格式化输出'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 = '';
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论