黄p网站_在线看中文字幕_亚洲电影免费观看_成人激情视频_欧美成视频_中国av在线

Response 對(duì)象

響應(yīng)客戶(hù)端的請(qǐng)求

生命周期

Response 對(duì)象在系統(tǒng)中以單例模式存在,自收到客戶(hù)端 HTTP 請(qǐng)求時(shí)自動(dòng)創(chuàng)建,直至請(qǐng)求結(jié)束自動(dòng)銷(xiāo)毀。Response 對(duì)象完全符合 PSR-7 中的所有規(guī)范。 其他細(xì)節(jié)方法,有興趣的同學(xué)可以在 IDE 中查看對(duì)應(yīng)的代碼。

在控制器中可以通過(guò) $this->response() 獲取到 Response 對(duì)象。

$response = $this->response();

核心方法

write

向客戶(hù)響應(yīng)數(shù)據(jù)。

// 向客戶(hù)端響應(yīng) 字符串?dāng)?shù)據(jù)
$this->response()->write('hello world');

注意:當(dāng)向客戶(hù)端響應(yīng)中文字符串時(shí),請(qǐng)務(wù)必設(shè)置響應(yīng)頭,并在 Content-Type 屬性中指定編碼,否則將顯示亂碼。

示例:

// 向客戶(hù)端響應(yīng) 中文字符串
// 設(shè)置響應(yīng)頭,并在 `Content-Type` 屬性中指定編碼
$this->response()->withHeader('Content-Type', 'text/html;charset=utf-8');
$this->response()->write('你好! easyswoole!');

// 向客戶(hù)端響應(yīng) json 字符串
$this->response()->withHeader('Content-Type', 'application/json;charset=utf-8');
$this->response()->write(json_encode(['name' => 'easyswoole']));

在控制器中可直接調(diào)用 $this->writeJson($statusCode = 200, $result = null, $msg = null) 方法向客戶(hù)端響應(yīng) json 字符串

示例:

// 在 `easyswoole` 控制器中,向客戶(hù)端響應(yīng) json 字符串
$this->writeJson(200, ['name' => 'easyswoole'], 'success!');

redirect

將請(qǐng)求重定向至指定的 URL

$this->response()->redirect("/newURL/index.html");

setCookie

向客戶(hù)端設(shè)置一個(gè) Cookie,用法與 PHP 原生的 setcookie 一致。

$this->response()->setCookie(string $name, $value = null, $expire = null,string $path = '/', string $domain = '', bool $secure = false, bool $httponly = false, string $samesite = '')

getSwooleResponse

獲取原始的 swoole_http_response 實(shí)例。

$swooleResponse = $this->response()->getSwooleResponse();

end

結(jié)束對(duì)該次 HTTP 請(qǐng)求響應(yīng),結(jié)束之后,無(wú)法再次向客戶(hù)端響應(yīng)數(shù)據(jù)。

$this->response()->end();

注意:和 Swoole 原生 swoole_http_response 實(shí)例的 end 方法有所區(qū)別。

isEndResponse

判斷該次 HTTP 請(qǐng)求是否結(jié)束響應(yīng),當(dāng)你不知道是否已經(jīng)結(jié)束響應(yīng)時(shí),可通過(guò)該方法判斷是否能再次向客戶(hù)端響應(yīng)數(shù)據(jù):

if (!$this->response()->isEndResponse()) {
    $this->response()->write('繼續(xù)發(fā)送數(shù)據(jù)');
}

withStatus

向客戶(hù)端發(fā)送 HTTP 狀態(tài)碼。

$this->response()->withStatus($statusCode);

注意:$statusCode 必須為標(biāo)準(zhǔn)的 HTTP 允許狀態(tài)碼,具體請(qǐng)見(jiàn) Http Message 中 的 Status 對(duì)象

withHeader

用于向 HTTP 客戶(hù)端發(fā)送一個(gè) header

$this->response()->withHeader('Content-Type', 'application/json;charset=utf-8');

其他方法

用于獲取響應(yīng)內(nèi)容,即需要響應(yīng)給客戶(hù)端的數(shù)據(jù)。一般用于在響應(yīng)客戶(hù)端之前記錄響應(yīng)日志之類(lèi)的業(yè)務(wù)。具體使用可查看框架的 afterRequest 事件

$this->response()->getBody()->__toString();

其他響應(yīng)

向客戶(hù)端響應(yīng)文件流,實(shí)現(xiàn)文件下載

  1. 實(shí)現(xiàn) excel 文件自動(dòng)下載

示例如下:在控制器中響應(yīng)客戶(hù)端,實(shí)現(xiàn) excel 文件自動(dòng)下載

<?php

namespace App\HttpController;

use EasySwoole\Http\AbstractInterface\Controller;

class Index extends Controller
{
    function index()
    {
        // 要下載 excel 文件的指定路徑,例如這里是項(xiàng)目根目錄下的 test.xlsx 文件
        $this->response()->sendFile(EASYSWOOLE_ROOT . '/test.xlsx');
        // 設(shè)置文件流內(nèi)容類(lèi)型,這里以 xlsx 為例
        $this->response()->withHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        // 設(shè)置要下載的文件名稱(chēng),一定要帶文件類(lèi)型后綴
        $this->response()->withHeader('Content-Disposition', 'attachment;filename=' . 'download_test.xlsx');
        $this->response()->withHeader('Cache-Control', 'max-age=0');
        $this->response()->end();
    }
}

訪問(wèn) http://localhost:9501/ 就會(huì)自動(dòng)下載 download_test.xlsx 文件了。

注意:這里必須使用 withHeader 設(shè)置響應(yīng)頭,一定不能使用 php-fpm 下的 header 函數(shù)設(shè)置。

主站蜘蛛池模板: 欧美色欧美亚洲另类七区 | 日韩免费视频一区二区 | 成人亚洲一区二区 | 97高清国语自产拍 | 欧美久久久久 | 欧美性受 | 久久99精品久久久久子伦 | 91精品国产91久久久久久不卡 | 中文字幕一区二区三区在线视频 | 久久在线视频 | 五月天婷婷色综合 | 日韩精品久久久久久 | 特级丰满少妇一级aaaa爱毛片 | 精品国产一区二区三区免费 | 黄色的视频免费 | 久久久久国产亚洲日本 | 天堂一区二区三区 | 国产专区在线 | 日韩不卡一区二区 | 日韩午夜| 久久精品视频亚洲 | 久久精品国产免费 | 亚洲日本韩国欧美 | 国产精品久久久久久久久久免费看 | 中国特黄毛片 | 欧美精品一区二区三区四区 | 不用播放器的av | 国产91亚洲精品 | 毛片免费视频 | 久久精品这里热有精品 | 97伦理网| 欧洲免费视频 | 曰韩在线 | www.久久| 91精品一区 | 久久久久久久久久久久久九 | 美日韩精品视频 | 亚洲wu码 | 精品国产91亚洲一区二区三区www | 美女天堂网 | 亚洲一区二区精品 |