<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                [TOC] * * * * * # 1 網絡響應 >>網絡響應,用來對服務器輸出內容的操作。 >tp5內置了各種格式的網絡響應。如view,json,xml,redirect等 >網絡響應 主要包括網絡響應信息的設置與網絡響應的發送。 ### $response->__construct() >>創建網絡響應對象 ~~~ public function __construct($data = '', $code = 200, array $header = [], $options = []) { $this->data($data); $this->header = $header; $this->code = $code; if (!empty($options)) { $this->options = array_merge($this->options, $options); } $this->contentType($this->contentType, $this->charset); } ~~~ >主要設置:網絡響應的數據內容,網絡響應頭部信息,網絡響應狀態碼,以及網絡響應類型。 ### Reponse::create() >>創建不同類型的響應對象 ~~~ public static function create($data = '', $type = '', $code = 200, array $header = [], $options = []) { $type = empty($type) ? 'null' : strtolower($type); $class = false !== strpos($type, '\\') ? $type : '\\think\\response\\' . ucfirst($type); if (class_exists($class)) { $response = new $class($data, $code, $header, $options); } else { $response = new static($data, $code, $header, $options); } return $response; } ~~~ >根據類型參數,創建不同類型的響應對象。具體類型的網絡響應對象實現在\response\目錄下。 # 2 網絡響應信息 >>網絡對象可以獲取和設置網絡響應信息 ## 2-1 設置響應對象信息 ### $response->data() >>設置網絡響應數據 ~~~ public function data($data) { $this->data = $data; return $this; } ~~~ ### $response->header() >>設置網絡響應頭 ~~~ public function header($name, $value = null) { if (is_array($name)) { $this->header = array_merge($this->header, $name); } else { $this->header[$name] = $value; } return $this; } ~~~ ### $response->code() >>設置網絡響應狀態碼 ~~~ public function code($code) { $this->code = $code; return $this; } ~~~ ### $response->content() >> 設置頁面輸出內容 ~~~ public function content($content) { if (null !== $content && !is_string($content) && !is_numeric($content) && !is_callable([ $content, '__toString', ]) ) { throw new \InvalidArgumentException(sprintf('variable type error: %s', gettype($content))); } $this->content = (string) $content; return $this; } ~~~ ### $response->lastModified() ~~~ public function lastModified($time) { $this->header['Last-Modified'] = $time; return $this; } ~~~ ### $response->expires() ~~~ public function expires($time) { $this->header['Expires'] = $time; return $this; } ~~~ ### $response->eTag() ~~~ public function eTag($eTag) { $this->header['ETag'] = $eTag; return $this; } ~~~ ### $response->cacheControl() ~~~ public function cacheControl($cache) { $this->header['Cache-control'] = $cache; return $this; } ~~~ ### $response->contentType() ~~~ public function contentType($contentType, $charset = 'utf-8') { $this->header['Content-Type'] = $contentType . '; charset=' . $charset; return $this; } ~~~ ## 2-2 獲取響應對象信息 ### $response->getHeader() >>獲取響應對象 頭部信息 ~~~ public function getHeader($name = '') { return !empty($name) ? $this->header[$name] : $this->header; } ~~~ ### $response->getData() >>獲取響應對象 原始數據 ~~~ public function getData() { return $this->data; } ~~~ ### $response->getContent() >>獲取響應對象 頁面內容 ~~~ public function getContent() { if (null == $this->content) { $content = $this->output($this->data); if (null !== $content && !is_string($content) && !is_numeric($content) && !is_callable([ $content, '__toString', ]) ) { throw new \InvalidArgumentException(sprintf('variable type error: %s', gettype($content))); } $this->content = (string) $content; } return $this->content; } ~~~ ### $response->getCode() >>獲取響應對象 狀態碼 ~~~ public function getCode() { return $this->code; } ~~~ ### $response->output() >>不同類型響應對象對數據的封裝處理 # 3 網絡響應發送 >> 網絡響應發送接口,將網絡響應信息發送到客戶端。 ### $response->send() ~~~ public function send() { // 處理輸出數據 $data = $this->getContent(); // Trace調試注入 if (Env::get('app_trace', Config::get('app_trace'))) { Debug::inject($this, $data); } if (!headers_sent() && !empty($this->header)) { // 發送狀態碼 http_response_code($this->code); // 發送頭部信息 foreach ($this->header as $name => $val) { header($name . ':' . $val); } } echo $data; if (function_exists('fastcgi_finish_request')) { // 提高頁面響應 fastcgi_finish_request(); } } ~~~ >首先輸出網絡響應狀態碼, 然后輸出網絡響應頭部信息, 最后輸出網絡響應的數據內容。 >其中網絡響應內容由不同響應對象進行封裝處理,封裝方法見下面的 網絡響應類型 # 4 網絡響應類型 >tp5內置json,jsonp,redirect,view,xml等響應類型 ## 4-1 json響應類型 ### $jsonresponse->output() ~~~ protected function output($data) { // 返回JSON數據格式到客戶端 包含狀態信息 $data = json_encode($data, $this->options['json_encode_param']); if ($data === false) { throw new \InvalidArgumentException(json_last_error_msg()); } return $data; } ~~~ ## 4-2 jsonp響應類型 ### $jsonpresponse->output() ~~~ protected function output($data) { // 返回JSON數據格式到客戶端 包含狀態信息 [當url_common_param為false時是無法獲取到$_GET的數據的,故使用Request來獲取<xiaobo.sun@qq.com>] $var_jsonp_handler = Request::instance()->param($this->options['var_jsonp_handler'], ""); $handler = !empty($var_jsonp_handler) ? $var_jsonp_handler : $this->options['default_jsonp_handler']; $data = json_encode($data, $this->options['json_encode_param']); if ($data === false) { throw new \InvalidArgumentException(json_last_error_msg()); } $data = $handler . '(' . $data . ');'; return $data; } ~~~ ## 4-3 redirect響應類型 >> redirect響應類型,主要對數據封裝為跳轉url地址。 ### $redirectresponse->output() >>redirect響應類型封裝接口 ~~~ protected function output($data) { $this->header['Location'] = $this->getTargetUrl(); return; } ~~~ ### $redirectresponse->with() >>使用session在跳轉url之間進行傳值 ~~~ public function with($name, $value = null) { if (is_array($name)) { foreach ($name as $key => $val) { Session::set($key, $val); } } else { Session::set($name, $value); } return $this; } ~~~ ### $redirectresponse->remember() >>使用session存儲當前跳轉url ~~~ public function remember() { Session::set('redirect_url', Request::instance()->url()); } ~~~ ### $redirectresponse->restore() >>跳轉到上次存儲的url ~~~ public function restore() { if (Session::has('redirect_url')) { $this->data = Session::get('redirect_url'); Session::delete('redirect_url'); } } ~~~ ## 4-4 view響應類型 >>view響應對象數據封裝。 ### $viewresponse->output() >>生成模板內容 ~~~ protected function output($data) { // 渲染模板輸出 return ViewTemplate::instance(Config::get('template'), Config::get('view_replace_str')) ->fetch($data, $this->vars, $this->replace); } ~~~ ### $viewresponse->assign() >>設置視圖模板變量 ~~~ public function assign($name, $value = '') { if (is_array($name)) { $this->vars = array_merge($this->vars, $name); return $this; } else { $this->vars[$name] = $value; } return $this; } ~~~ ### $viewresponse->getVars() >>獲取視圖模板變量 ~~~ public function getVars($name = null) { if (is_null($name)) { return $this->vars; } else { return isset($this->vars[$name]) ? $this->vars[$name] : null; } } ~~~ ### $viewresponse->replace() >>視圖內容替換 ~~~ public function replace($content, $replace = '') { if (is_array($content)) { $this->replace = array_merge($this->replace, $content); } else { $this->replace[$content] = $replace; } return $this; } ~~~ ## 4-5 xml響應類型 ### $xmlresponse->output() >>封裝為xml接口 ~~~ protected function output($data) { // XML數據轉換 return $this->xmlEncode($data, $this->options['root_node'], $this->options['item_node'], $this->options['root_attr'], $this->options['item_key'], $this->options['encoding']); } ~~~ ### $xmlresponse->xmlEncode() >>封裝為xml格式 ~~~ protected function xmlEncode($data, $root, $item, $attr, $id, $encoding) { if (is_array($attr)) { $array = []; foreach ($attr as $key => $value) { $array[] = "{$key}=\"{$value}\""; } $attr = implode(' ', $array); } $attr = trim($attr); $attr = empty($attr) ? '' : " {$attr}"; $xml = "<?xml version=\"1.0\" encoding=\"{$encoding}\"?>"; $xml .= "<{$root}{$attr}>"; $xml .= $this->dataToXml($data, $item, $id); $xml .= "</{$root}>"; return $xml; } ~~~ ### $xmlresponse->dataToXml() >>數據為xml格式 ~~~ protected function dataToXml($data, $item, $id) { $xml = $attr = ''; foreach ($data as $key => $val) { if (is_numeric($key)) { $id && $attr = " {$id}=\"{$key}\""; $key = $item; } $xml .= "<{$key}{$attr}>"; $xml .= (is_array($val) || is_object($val)) ? $this->dataToXml($val, $item, $id) : $val; $xml .= "</{$key}>"; } return $xml; } ~~~
                  <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>

                              哎呀哎呀视频在线观看