响应的发送是通过index.php中的$response->send();实现的
vendor\symfony\http-foundation\Response.php
1 public function send() 2 { 3 $this->sendHeaders(); 4 $this->sendContent(); 5 6 if (function_exists('fastcgi_finish_request')) { 7 fastcgi_finish_request(); 8 } elseif (!\in_array(PHP_SAPI, array('cli', 'phpdbg'), true)) { 9 static::closeOutputBuffers(0, true); 10 } 11 12 return $this; 13 }
1 public function sendHeaders() 2 { 3 // headers have already been sent by the developer 4 if (headers_sent()) { 5 return $this; 6 } 7 8 // headers 9 foreach ($this->headers->allPreserveCase() as $name => $values) { 10 foreach ($values as $value) { 11 header($name.': '.$value, false, $this->statusCode); 12 } 13 } 14 15 // status 16 header(sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText), true, $this->statusCode); 17 18 return $this; 19 }
public function sendContent() { echo $this->content; return $this; }
响应和发送分别是头信息发送和主题内容发送
头包括:状态行,首部字段和cookie的发送,状态行和首部字段是通过header()函数完成的,cookie发送是通过setcookie函数完成的
程序的终止
//程序的终止(index.php)
$kernel->terminate($request, $response);
public function terminate($request, $response) { $this->terminateMiddleware($request, $response); $this->app->terminate(); }
protected function terminateMiddleware($request, $response) { $middlewares = $this->app->shouldSkipMiddleware() ? [] : array_merge( $this->gatherRouteMiddleware($request), $this->middleware ); foreach ($middlewares as $middleware) { if (! is_string($middleware)) { continue; } list($name) = $this->parseMiddleware($middleware); $instance = $this->app->make($name); if (method_exists($instance, 'terminate')) { $instance->terminate($request, $response); } } }
\vendor\laravel\framework\src\Illuminate\Foundation\Application.php
public function terminate() { foreach ($this->terminatingCallbacks as $terminating) { $this->call($terminating); } }
本文来自博客园,作者:孙龙-程序员,转载请注明原文链接:https://www.cnblogs.com/sunlong88/p/9365049.html