Laravel 發送響應

Laravel 發送響應

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

    if (function_exists('fastcgi_finish_request')) {
        fastcgi_finish_request();
    } elseif ('cli' !== PHP_SAPI) {
        static::closeOutputBuffers(0, true);
    }

    return $this;
}

發送響應頭

public function sendHeaders()
{
    if (headers_sent()) {
        return $this;
    }

    if (!$this->headers->has('Date')) {
        $this->setDate(\DateTime::createFromFormat('U', time()));
    }

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

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

    foreach ($this->headers->getCookies() as $cookie) {
        if ($cookie->isRaw()) {
            setrawcookie($cookie->getName(), $cookie->getValue(), $cookie->getExpiresTime(), $cookie->getPath(), $cookie->getDomain(), $cookie->isSecure(), $cookie->isHttpOnly());
        } else {
            setcookie($cookie->getName(), $cookie->getValue(), $cookie->getExpiresTime(), $cookie->getPath(), $cookie->getDomain(), $cookie->isSecure(), $cookie->isHttpOnly());
        }
    }

    return $this;
}

發送響應體

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

    return $this;
}

發送到客戶端

// 如果 fastcgi 模式,則結束客戶端響應後異步執行服務器端的後續任務,例如中間件的 terminate 方法和事件等
if (function_exists('fastcgi_finish_request')) {
    fastcgi_finish_request();
} elseif ('cli' !== PHP_SAPI) {
    static::closeOutputBuffers(0, true);
}
public static function closeOutputBuffers($targetLevel, $flush)
{
    $status = ob_get_status(true);
    $level = count($status);
    $flags = defined('PHP_OUTPUT_HANDLER_REMOVABLE') ? PHP_OUTPUT_HANDLER_REMOVABLE | ($flush ? PHP_OUTPUT_HANDLER_FLUSHABLE : PHP_OUTPUT_HANDLER_CLEANABLE) : -1;

    while ($level-- > $targetLevel && ($s = $status[$level]) && (!isset($s['del']) ? !isset($s['flags']) || $flags === ($s['flags'] & $flags) : $s['del'])) {
        if ($flush) {
            ob_end_flush();
        } else {
            ob_end_clean();
        }
    }
}

後續動做

執行中間件的 terminate 方法和事件等服務器

至此,框架核心流程所有走完cookie

相關文章
相關標籤/搜索