【Laravel3.0.0源码阅读分析】响应类response.php
发布日期:2021-06-30 20:44:46 浏览次数:2 分类:技术文章

本文共 8554 字,大约阅读时间需要 28 分钟。

'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. * 创建一个新的响应实例。 * * // 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')); * * * @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. * 创建一个包含视图的新响应实例。 * * // 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')); * * * @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. * * * // Create a 404 response * return Response::error('404'); * * // Create a 404 response with data * return Response::error('404', array('message' => 'Not Found')); * * * @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. * 创建一个新的下载响应实例。 * * // 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'); * * * @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://liushilong.blog.csdn.net/article/details/117399119 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:【Laravel3.0.0源码阅读分析】请求类request.php
下一篇:【Laravel3.0.0源码阅读分析】section类section.php

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2024年04月06日 01时13分22秒