<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                [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() 重定向操作 數據輸出的使用方法見 使用范例的 輸出控制
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看