中文在线一区二区_欧美在线综合_久久久久久综合_欧美一区二区三区视频_国产免费看_国产福利精品一区

Response 對象

響應客戶端的請求

生命周期

Response 對象在系統中以單例模式存在,自收到客戶端 HTTP 請求時自動創建,直至請求結束自動銷毀。Response 對象完全符合 PSR-7 中的所有規范。 其他細節方法,有興趣的同學可以在 IDE 中查看對應的代碼。

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

$response = $this->response();

核心方法

write

向客戶響應數據。

// 向客戶端響應 字符串數據
$this->response()->write('hello world');

注意:當向客戶端響應中文字符串時,請務必設置響應頭,并在 Content-Type 屬性中指定編碼,否則將顯示亂碼。

示例:

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

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

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

示例:

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

redirect

將請求重定向至指定的 URL

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

setCookie

向客戶端設置一個 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 實例。

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

end

結束對該次 HTTP 請求響應,結束之后,無法再次向客戶端響應數據。

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

注意:和 Swoole 原生 swoole_http_response 實例的 end 方法有所區別。

isEndResponse

判斷該次 HTTP 請求是否結束響應,當你不知道是否已經結束響應時,可通過該方法判斷是否能再次向客戶端響應數據:

if (!$this->response()->isEndResponse()) {
    $this->response()->write('繼續發送數據');
}

withStatus

向客戶端發送 HTTP 狀態碼。

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

注意:$statusCode 必須為標準的 HTTP 允許狀態碼,具體請見 Http Message 中 的 Status 對象。

withHeader

用于向 HTTP 客戶端發送一個 header

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

其他方法

用于獲取響應內容,即需要響應給客戶端的數據。一般用于在響應客戶端之前記錄響應日志之類的業務。具體使用可查看框架的 afterRequest 事件

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

其他響應

向客戶端響應文件流,實現文件下載

  1. 實現 excel 文件自動下載

示例如下:在控制器中響應客戶端,實現 excel 文件自動下載

<?php

namespace App\HttpController;

use EasySwoole\Http\AbstractInterface\Controller;

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

訪問 http://localhost:9501/ 就會自動下載 download_test.xlsx 文件了。

注意:這里必須使用 withHeader 設置響應頭,一定不能使用 php-fpm 下的 header 函數設置。

主站蜘蛛池模板: 亚洲成人免费 | 免费观看一级一片 | 亚洲国产精品一区二区久久,亚洲午夜 | 成人一级黄色大片 | 狠久久 | 亚洲黄色特级片 | 中文字幕一二三区 | 亚洲热视频在线观看 | 在线观看中文字幕av | 成年人免费网站 | 爆操av| 国产美女久久久 | 日韩成人片 | 欧美日韩国产精品一区 | 欧美日韩综合 | 米奇777超碰欧美日韩亚洲 | 亚洲综合中文网 | 一特黄a大片免费视频 视频 | 午夜激情影院 | 欧美二区在线 | 一级片在线观看网站 | 操网| 久久免费福利视频 | 免费三级国产 | 国产福利视频在线观看 | 精品久久久久久久人人人人传媒 | 久久国产精品99久久久久久老狼 | 国产成人精品一区二区在线 | 玖玖国产精品视频 | 91精品在线看 | 97天堂| 国产亚洲精品美女久久久久久久久久 | 最新国产视频 | 草草视频网站 | 99热99 | 精品国产欧美一区二区 | 午夜高清视频 | 国产精品乱码一区二区三区 | 欧美日韩成人在线观看 | 午夜视频在线观看网站 | 狠狠撸在线 |