• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
孙龙 程序员
少时总觉为人易,华年方知立业难
博客园    首页    新随笔    联系   管理    订阅  订阅
laravel响应的发送和程序终止

响应的发送是通过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

posted on 2018-07-25 11:35  孙龙-程序员  阅读(555)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3