[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() 重定向操作
數據輸出的使用方法見 使用范例的 輸出控制
- 更新記錄
- 概述
- 文件索引
- 函數索引
- 章節格式
- 框架流程
- 前:章節說明
- 主:(index.php)入口
- 主:(start.php)框架引導
- 主:(App.php)應用啟動
- 主:(App.php)應用調度
- C:(Controller.php)應用控制器
- M:(Model.php)數據模型
- V:(View.php)視圖對象
- 附:(App.php)應用啟動
- 附:(base.php)全局變量
- 附:(common.php)模式配置
- 附:(convention.php)全局配置
- 附:(Loader.php)自動加載器
- 附:(Build.php)自動生成
- 附:(Hook.php)監聽回調
- 附:(Route.php)全局路由
- 附:(Response.php)數據輸出
- 附:(Log.php)日志記錄
- 附:(Exception.php)異常處理
- 框架工具
- 另:(helper.php)輔助函數
- 另:(Cache.php)數據緩存
- 另:(Cookie.php)cookie操作
- 另:(Console.php)控制臺
- 另:(Debug.php)開發調試
- 另:(Error.php)錯誤處理
- 另:(Url.php)Url操作文件
- 另:(Loader.php)加載器實例化
- 另:(Input.php)數據輸入
- 另:(Lang.php)語言包管理
- 另:(ORM.php)ORM基類
- 另:(Process.php)進程管理
- 另:(Session.php)session操作
- 另:(Template.php)模板解析
- 框架驅動
- D:(\config)配置解析
- D:(\controller)控制器擴展
- D:(\model)模型擴展
- D:(\db)數據庫驅動
- D:(\view)模板解析
- D:(\template)模板標簽庫
- D:(\session)session驅動
- D:(\cache)緩存驅動
- D:(\console)控制臺
- D:(\process)進程擴展
- T:(\traits)Trait目錄
- D:(\exception)異常實現
- D:(\log)日志驅動
- 使用范例
- 服務器與框架的安裝
- 控制器操作
- 數據模型操作
- 視圖渲染控制
- MVC開發初探
- 模塊開發
- 入口文件定義全局變量
- 運行模式開發
- 框架配置
- 自動生成應用
- 事件與插件注冊
- 路由規則注冊
- 輸出控制
- 多種應用組織
- 綜合應用
- tp框架整合后臺auto架構快速開發
- 基礎原理
- php默認全局變量
- php的魔術方法
- php命名空間
- php的自動加載
- php的composer
- php的反射
- php的trait機制
- php設計模式
- php的系統時區
- php的異常錯誤
- php的輸出控制
- php的正則表達式
- php的閉包函數
- php的會話控制
- php的接口
- php的PDO
- php的字符串操作
- php的curl
- 框架心得
- 心:整體結構
- 心:配置詳解
- 心:加載器詳解
- 心:輸入輸出詳解
- 心:url路由詳解
- 心:模板詳解
- 心:模型詳解
- 心:日志詳解
- 心:緩存詳解
- 心:控制臺詳解
- 框架更新
- 4.20(驗證類,助手函數)
- 4.27(新模型Model功能)
- 5.4(新數據庫驅動)
- 7.28(自動加載)