【Laravel3.0.0源码阅读分析】响应类response.php

【Laravel3.0.0源码阅读分析】响应类response.php,第1张

概述<?phpnamespaceLaravel;//响应类classResponse{ /** *Thecontentoftheresponse. *响应的内容。 *@varmixed */ public$content; /** *TheHTTPstatuscodeoftheresponse. *响应的HTTP状态代码。 *@varint */ public$status=
<?PHP namespace Laravel;// 响应类class Response {	/**	 * The content of the response.	 * 响应的内容。	 * @var mixed	 */	public $content;	/**	 * The http status code of the response.	 * 响应的 http 状态代码。	 * @var int	 */	public $status = 200;	/**	 * The response headers.	 * 响应头。	 * @var array	 */	public $headers = array();	/**	 * http status codes.	 * http 状态代码。	 * @var array	 */	public static $statuses = array(		100 => 'Continue', // 继续		101 => 'Switching Protocols', // 交换协议		200 => 'OK', // OK		201 => 'Created', // 已创建		202 => 'Accepted', // 已接收		203 => 'Non-Authoritative information', // 非权威信息		204 => 'No Content', // 无内容		205 => 'reset Content', // 重置内容		206 => 'Partial Content', // 部分内容		207 => 'Multi-Status', // 多状态		300 => 'Multiple Choices', // 多项选择		301 => 'Moved Permanently', // 永久移动		302 => 'Found', // 找到		303 => 'See Other', // 查看其他		304 => 'Not ModifIEd', // 未修改		305 => 'Use Proxy', // 使用代理服务器		307 => 'Temporary Redirect', // 临时重定向		400 => 'Bad Request', // 错误的请求		401 => 'Unauthorized', // 未经授权		402 => 'Payment required', // 需要付款		403 => 'ForbIDden', // 禁止的		404 => 'Not Found', // 未找到		405 => 'Method Not Allowed', // 不允许的方法		406 => 'Not Acceptable',		407 => 'Proxy Authentication required', // 不能接受的		408 => 'Request Timeout', // 请求超时		409 => 'Conflict', // 冲突		410 => 'Gone', // 不见了		411 => 'Length required', // 长度要求		412 => 'Precondition Failed', // 先决条件失败		413 => 'Request Entity Too Large', // 请求的实体太大		414 => 'Request-URI Too Long', // 请求 URI 太长		415 => 'Unsupported Media Type', // 不支持的媒体类型		416 => 'Requested Range Not Satisfiable', // 请求的范围不满足		417 => 'Expectation Failed', // 预期失败		422 => 'Unprocessable Entity', // 不可处理的实体		423 => 'Locked', // 锁定		424 => 'Failed Dependency', // 依赖失败		500 => 'Internal Server Error', // 内部服务器错误		501 => 'Not Implemented', // 未实现		502 => 'Bad Gateway', // 错误的网关		503 => 'Service Unavailable', // 暂停服务		504 => 'Gateway Timeout', // 网关超时		505 => 'http Version Not Supported', // 不支持 http 版本		507 => 'InsufficIEnt Storage', // 存储空间不足		509 => 'BanDWIDth limit Exceeded' // 超出带宽限制	);	/**	 * Create a new response instance.	 * 创建一个新的响应实例。	 * @param  mixed  $content	 * @param  int    $status	 * @param  array  $headers	 * @return voID	 */	public function __construct($content, $status = 200, $headers = array())	{		$this->status = $status;		$this->content = $content;		$this->headers = $headers;	}	/**	 * Create a new response instance.	 * 创建一个新的响应实例。	 * <code>	 *		// Create a response instance with string content	 *		return Response::make(Json_encode($user));	 *	 *		// Create a response instance with a given status	 *		return Response::make('Not Found', 404);	 *	 *		// Create a response with some custom headers	 *		return Response::make(Json_encode($user), 200, array('header' => 'value'));	 * </code>	 *	 * @param  mixed     $content	 * @param  int       $status	 * @param  array     $headers	 * @return Response	 */	public static function make($content, $status = 200, $headers = array())	{		return new static($content, $status, $headers);	}	/**	 * Create a new response instance containing a vIEw.	 * 创建一个包含视图的新响应实例。	 * <code>	 *		// Create a response instance with a vIEw	 *		return Response::vIEw('home.index');	 *	 *		// Create a response instance with a vIEw and data	 *		return Response::vIEw('home.index', array('name' => 'Taylor'));	 * </code>	 *	 * @param  string    $vIEw	 * @param  array     $data	 * @return Response	 */	public static function vIEw($vIEw, $data = array())	{		return new static(VIEw::make($vIEw, $data));	}	/**	 * Create a new error response instance.	 * 创建一个新的错误响应实例。	 * The response status code will be set using the specifIEd code.	 * 将使用指定的代码设置响应状态代码。	 * The specifIEd error should match a vIEw in your vIEws/error directory.	 *	 * <code>	 *		// Create a 404 response	 *		return Response::error('404');	 *	 *		// Create a 404 response with data	 *		return Response::error('404', array('message' => 'Not Found'));	 * </code>	 *	 * @param  int       $code	 * @param  array     $data	 * @return Response	 */	public static function error($code, $data = array())	{		return new static(VIEw::make('error.'.$code, $data), $code);	}	/**	 * Create a new download response instance.	 * 创建一个新的下载响应实例。	 * <code>	 *		// Create a download response to a given file	 *		return Response::download('path/to/file.jpg');	 *	 *		// Create a download response with a given file name	 *		return Response::download('path/to/file.jpg', 'your_file.jpg');	 * </code>	 *	 * @param  string    $path	 * @param  string    $name	 * @param  array     $headers	 * @return Response	 */	public static function download($path, $name = null, $headers = array())	{		if (is_null($name)) $name = basename($path);		$headers = array_merge(array(			'Content-Description'       => 'file Transfer',			'Content-Type'              => file::mime(file::extension($path)),			'Content-disposition'       => 'attachment; filename="'.$name.'"',			'Content-transfer-encoding' => 'binary',			'Expires'                   => 0,			'Cache-Control'             => 'must-revalIDate, post-check=0, pre-check=0',			'Pragma'                    => 'public',			'Content-Length'            => file::size($path),		), $headers);		return new static(file::get($path), 200, $headers);	}	/**	 * Prepare a response from the given value.	 * 从给定值准备响应。	 * If the value is not a response, it will be converted into a response	 * instance and the content will be cast to a string.	 * 如果该值不是响应,则将其转换为响应实例,并将内容转换为字符串。	 * @param  mixed     $response	 * @return Response	 */	public static function prepare($response)	{		if ( ! $response instanceof Response)		{			$response = new static($response);		}		// We'll need to force the response to be a string before closing the session,		// since the developer may be using the session within a vIEw, and we can't		// age the flash data until the vIEw is rendered.		// 我们需要在关闭会话之前强制响应为字符串,因为开发人员可能正在视图中使用会话,并且在呈现视图之前我们无法对 Flash 数据进行老化。		// Since this method is used by both the Route and Controller classes, it is		// a convenIEnt spot to cast the application response to a string before it		// is returned to the main request handler.        // 由于 Route 和 Controller 类都使用此方法,        // 因此在将应用程序响应返回到主请求处理程序之前将应用程序响应转换为字符串是一个方便的位置。		$response->render();		return $response;	}	/**	 * Convert the content of the Response to a string and return it.	 * 将 Response 的内容转换为字符串并返回。	 * @return string	 */	public function render()	{	    // is_object-检测是否为对象 method_exists-检测类的方法是否存在		if (is_object($this->content) and method_exists($this->content, '__toString'))		{			$this->content = $this->content->__toString();		}		else		{			$this->content = (string) $this->content;		}		return $this->content;	}	/**	 * Send the headers and content of the response to the browser.	 * 将响应的标头和内容发送到浏览器。	 * @return voID	 */	public function send()	{		if ( ! headers_sent()) $this->send_headers();		echo (string) $this->content;	}	/**	 * Send all of the response headers to the browser.	 * 将所有响应头发送到浏览器。	 * @return voID	 */	public function send_headers()	{		// If the server is using FastCGI, we need to send a slightly different		// protocol and status header than we normally would. Otherwise it will		// not call any custom scripts setup to handle 404 responses.		// 如果服务器使用的是 FastCGI,我们需要发送一个稍微不同的协议和状态头比我们通常会。        // 否则它不会调用任何自定义脚本设置来处理 404 响应。		// The status header will contain both the code and the status message,		// such as "OK" or "Not Found". For typical servers, the http protocol		// will also be included with the status.        // 状态标题将包含代码和状态消息,例如“OK”或“Not Found”。 对于典型的服务器,http 协议也将包含在状态中。		if (isset($_SERVER['FCGI_SERVER_VERSION']))		{			header('Status: '.$this->status.' '.$this->message());		}		else		{			header(Request::protocol().' '.$this->status.' '.$this->message());		}		// If the content type was not set by the developer, we will set the		// header to a default value that indicates to the browser that the		// response is HTML and that it uses the default enCoding.		if ( ! isset($this->headers['Content-Type']))		{			$enCoding = Config::get('application.enCoding');			$this->header('Content-Type', 'text/HTML; charset='.$enCoding);		}		// Once the framework controlled headers have been sentm, we can		// simply iterate over the developer's headers and send each one		// back to the browser for the response.        // 如果开发人员未设置内容类型,我们会将标头设置为默认值,以向浏览器指示响应是 HTML 并使用默认编码。		foreach ($this->headers as $name => $value)		{			header("{$name}: {$value}", true);		}	}	/**	 * Get the status code message for the response.	 * 获取响应的状态代码消息。	 * @return string	 */	public function message()	{		return static::$statuses[$this->status];	}	/**	 * Add a header to the array of response headers.	 * 将标头添加到响应标头数组中。	 * @param  string    $name	 * @param  string    $value	 * @return Response	 */	public function header($name, $value)	{		$this->headers[$name] = $value;		return $this;	}	/**	 * Set the response status code.	 * 设置响应状态代码。	 * @param  int       $status	 * @return Response	 */	public function status($status)	{		$this->status = $status;		return $this;	}}

github地址: https://github.com/liu-shilong/laravel3-scr     

总结

以上是内存溢出为你收集整理的【Laravel3.0.0源码阅读分析】响应类response.php全部内容,希望文章能够帮你解决【Laravel3.0.0源码阅读分析】响应类response.php所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1001834.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存