<?php// +----------------------------------------------------------------------// | thinkphp [ we can do it just think ]// +--------------------------------- -------------------------------------// | copyright (c) 2006~2016 http://thinkphp.cn all rights reserved.// +----------------------------------------------------------------- -----// | licensed ( http://www.apache.org/licenses/LICENSE-2.0 )// +----------- -----------------------------------------------------------// | author: liu21st <[email protected]>// +----------------------------------------------------------------------Namespace think;// using Namespaces Use think\config;use think\debug;use think\env;use think\ response\json as jsonresponse;use think\response\jsonp as jsonpresponse;use think\response\redirect&Nbsp;as redirectresponse;use think\response\view as viewresponse;use think\response\ xml as xmlresponse;// use Other resources class response{ // raw data protected $data;// element data // current contenttype protected $contentType = ' text/html ';// current type content type // Character Set protected $charset = ' utf-8 ';// Protected character Set //status protected $code = 200;// Return status // output parameters protected $options = [];// Parameters for Output // header parameters protected $header = [];// parameters protected $content = null;// content /** * schema functions * @access public * @param mixed $data output Data * @param int $code * @param array $ header * @param array $options OUTPUT Parameters */ public function __construct ($data = ', $code = 200, array $header = [], $options = []) {// Constructors initialization Various data $this->data ($data);// processing Data combination $this->header = $header;// header information Processing $this->code = $code;// Return codes Default 200 if (!empty ($options)) {// if is not empty merge options $this->options = array_merge ($this- >options, $options); } $this->contenttype ($this->contenttype, $this->charset);// finishing types About Content } /** * Create response Objects * @access public * @ param mixed $data output data * @param string $type Output types * @param int $code * @param array $header * @param array $options OUTPUT parameters * @return response| jsonresponse| viewresponse| xmlresponse| redirectresponse| jsonpresponse */ public static function Create ($data = ', $type = ', $code = 200, array $header = [], $options = []) {// constructors initializing objects Create Object $type = empty ($type) ? ' null ' : strtolower ($type);// if is empty null and lowercase $class = false !== strpos ($type, ' \ \ ') ? $type : ' \\think\\ Response\\ ' . ucfirst ($type);// subdivided into other internal specific scheduling classes if (Class_exists ($class)) {// there is a corresponding processing branch $response = new $class ($data, $code, $header, $ Options); } else {// otherwise with default static Functions $response = new Static ($data, $code, $header, $options); } return $response;// return with simple single-row mode is not the same, haha } /** * send data to client * @access public * @return mixed * @throws \InvalidArgumentException */ public function send () { // Processing output Data $data = $this->getcontent ();// processing output data // trace Debug Injection if (Env::get (' App_trace ', config::get (' App_trace ')) {// environment:: get debug get Configuration About debug debug::inject ($this, $data);// debug::inject () Inject current } if (!headers_sent () && !empty ($this->header)) {// Send header message // Send status code if required http_response_code ($this->code);// Return http status code // Send header information &NBSP;&NBSP;&NBSP;&Nbsp; foreach ($this->header as $name = > $val) {// Traverse header information header ($name . ': ' . $val);// use header tag options follow Values } } echo $data;// Direct printing data if (function_exists (' fastcgi_finish_request ')) { // Improve page Response fastcgi_finish_request (); // This function flushes (flush) all the response data to the client and ends the request. This enables a task that requires a large amount of time to run after the client ends the connection. // Save Client output wait } } /** * processing Data * @access protected * @param mixed $data Data to process * @return mixed */ protected function output ($data) {// Process data output Direct return data return $data Parameters of the; } /** * output * @access public * @param mixed $ options output Parameters * @return $this */ public Function options ($options = []) {// output Parameters $this->options = array_merge ($this->options, $options); return $this; }// Code of command chain Fusion /** * output Data setup * @access public * @param mixed $data output data * @return $this */ Public function data ($data) { $this->data = $data; return $this; }// settings data command chain /** * Setting the response header * @access public * @param string|array $name Parameter name * @param string $ value parameter values * @return $this */ public function header ($name, $value = null) { if (Is_array ($name)) { $this->header = array_merge ($this header, $name); } else { $this->header[$name] = $value; } return $this; }// Settings Header information Command chain /** * Settings page output content * @param $content * @return $this */ 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; }// Settings compliant page content command chain /** * Send HTTP status * @param integer $code Status code * @return $this */ public function code ($code) { $this->code = $code; return $this; } /** * lastmodified * @ param string $time * @return $this */ &nbsP;public function lastmodified ($time) { $this->header[' last-modified '] = $time; return $this; } /** * Expires * @param string $time * @return $this */ public function Expires ($time) { $this->header[' Expires '] = $time; return $this; } /** * ETag * @param string $eTag * @return $this */ public function etag ($ETAG) { $this->header[' ETag '] = $eTag; return $ this; } /** * Page Cache control * @param string $cache Status code * @ return $this */ public function CacheControl ($cache) { $this->header[ ' Cache-control '] = $cache; return $this; } /** * page Output Type * @param string $contentType Output Type * @param string $charset &NBSP;&Nbsp; output Codes * @return $this */ public function contenttype ($contentType, $charset = ' Utf-8 ') { $this->header[' Content-type '] = $contentType . '; charset= ' . $charset; return $this; } /** * Get header information * @param string $name Header name * @return mixed */ public function getheader ($name = ") { return !empty ($name) ? $this->header[$name] : $this- >header; } /** * Get Raw Data * @ Return mixed */ public function getdata () { return $this->data; } /** * Get output Data * @return mixed */ 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; }// Throw Exception &NBSP;&NBSP;&Nbsp; /** * Get Status Codes * @return Integer */ public function getcode () { return $this->code; }}
This article is from the "Focus on PHP Group number: 414194301" blog, please be sure to keep this source http://jingshanls.blog.51cto.com/3357095/1890943
[Li Jingshan php] every day tp5-20170204|thinkphp5-response.php