namespace Illuminate\Http;
use ArrayObject;
use JsonSerializable;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Renderable;
use Symfony\Component\HttpFoundation\Response as BaseResponse;
class Response extends BaseResponse
{
use ResponseTrait, Macroable {
Macroable::__call as macroCall;
}
}
複製代碼
public function __construct($content = '', int $status = 200, array $headers = [])
{
$this->headers = new ResponseHeaderBag($headers);
// "設置內容到$content成員中"
$this->setContent($content);
// "設置statusCode,若是<100 || >=600 則拋異常,不然設置頭部信息"
$this->setStatusCode($status);
// "設置HTTP協議版本號"
$this->setProtocolVersion('1.0');
}
複製代碼
• 繼承自Symfony中的方法
public function send()
{
// "返回header信息"
$this->sendHeaders();
// "echo $this->content;"
$this->sendContent();
if (\function_exists('fastcgi_finish_request')) {
// "刷新全部數據到客戶端,同時結束請求."
fastcgi_finish_request();
} elseif (!\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true)) {
// "不存在fastcgi_finish_request()方法執行"
static::closeOutputBuffers(0, true);
}
return $this;
}
public static function closeOutputBuffers(int $targetLevel, bool $flush)
{
$status = ob_get_status(true);
$level = \count($status);
$flags = PHP_OUTPUT_HANDLER_REMOVABLE | ($flush ? PHP_OUTPUT_HANDLER_FLUSHABLE : PHP_OUTPUT_HANDLER_CLEANABLE);
while ($level-- > $targetLevel && ($s = $status[$level]) && (!isset($s['del']) ? !isset($s['flags']) || ($s['flags'] & $flags) === $flags : $s['del'])) {
if ($flush) {
ob_end_flush();
} else {
ob_end_clean();
}
}
}
複製代碼
執行完成send()方法以後fastcgi處理完成當前的請求,php
並將緩衝區的數據以及頭部信息輸出到瀏覽器。瀏覽器
到此響應輸出完成。bash
關於框架如何在啓動的過程當中構造出來Response對象在後面的章節會詳細講解,框架
當前是屬於分解Response類來了解裏面都實現了什麼功能。函數