编程学习网 > PHP技术 > laravel > Laravel终止:laravel响应的发送和程序的终止详情
2021
04-29

Laravel终止:laravel响应的发送和程序的终止详情



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响应的发送和程序的终止就先讲到这里,想了解更多相关内容咨询欢迎前往并持续关注编程学习网

扫码二维码 获取免费视频学习资料

Python编程学习

查 看2022高级编程视频教程免费获取