Laravel响应的发送是通过index.php中的$response->send();实现的

vendor\symfony\http-foundation\Response.php

public function send() 
{  
     $this->sendHeaders();  
     $this->sendContent();

      if (function_exists('fastcgi_finish_request')) {  
          fastcgi_finish_request();  
      } elseif (!\in_array(PHP_SAPI, array('cli', 'phpdbg'), true)) {  
           static::closeOutputBuffers(0, true); 
      } 

      return $this; 
}

 

public function sendHeaders()  
{  
      // headers have already been sent by the developer  
      if (headers_sent()) {  
          return $this;  
      }  

      // headers  
      foreach ($this->headers->allPreserveCase() as $name => $values) { 
            foreach ($values as $value) {
                  header($name.': '.$value, false, $this->statusCode); 
            } 
      } 

      // status 
      header(sprintf('HTTP/%s %s %s', $this->version, $this->statusCode,$this->statusText), true, $this->statusCode); 

      return $this; 
}

 

public function sendContent()
{
echo $this->content;
return $this;
}

Laravel响应的发送分别是头信息发送和主题内容发送

头包括:状态行,首部字段和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);
}
}

这篇Laravel响应的发送和程序的终止就先讲到这里,想了解更多相关内容咨询欢迎前往并持续关注编程学习网。