laravel響應的發送和程序終止

響應的發送是經過index.php中的$response->send();實現的php

vendor\symfony\http-foundation\Response.phplaravel

 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

頭包括:狀態行,首部字段和cookie的發送,狀態行和首部字段是經過header()函數完成的,cookie發送是經過setcookie函數完成的app

 

程序的終止

//程序的終止(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);
        }
    }
相關文章
相關標籤/搜索