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

Request 對象

接收客戶端的 HTTP 請求對象

生命周期

Request 對象在系統(tǒng)中以單例模式存在,自收到客戶端 HTTP 請求時自動創(chuàng)建,直至請求結(jié)束自動銷毀。Request 對象完全符合 PSR-7 中的所有規(guī)范。

核心方法

getRequestParam()

用于獲取用戶通過 POST 或者 GET 提交的參數(shù)(注意:若 POSTGET 存在同鍵名參數(shù),則以POST 為準)。

示例:

// 在控制器中 可以通過 $this->request() 獲取到 Request 對象
// $request = $this->request();

// 獲取 `POST` 或者 `GET` 提交的所有參數(shù)
$data = $request->getRequestParam();
var_dump($data);

// 獲取 `POST` 或者 `GET` 提交的單個參數(shù)
$orderId = $request->getRequestParam('orderId');
var_dump($orderId);

// 獲取 `POST` 或者 `GET` 提交的多個參數(shù)
$mixData = $request->getRequestParam("orderId","type");
var_dump($mixData);

getSwooleRequest()

獲取當前的 swoole_http_request 對象。

getCookieParams()

獲取 HTTP 請求中的 cookie 信息

// 獲取所有 `cookie` 信息
$all = $request->getCookieParams();
var_dump($all);

// 獲取單個 `cookie` 信息
$who = $request->getCookieParams('who');
var_dump($who);

getUploadedFiles()

獲取客戶端上傳的全部文件信息。

// 獲取一個上傳文件,返回的是一個 \EasySwoole\Http\Message\UploadFile 的對象
$img_file = $request->getUploadedFile('img');

// 獲取全部上傳文件返回包含 \EasySwoole\Http\Message\UploadFile 對象的數(shù)組
$data = $request->getUploadedFiles();
var_dump($data);

點擊查看 UploadFile對象

getBody()

獲取以非 form-datax-www-form-urlenceded 編碼格式 POST 提交的原始數(shù)據(jù),相當于PHP中的 $HTTP_RAW_POST_DATA

獲得 get 內(nèi)容

$get = $request->getQueryParams();

獲得 post 內(nèi)容

$post = $request->getParsedBody();

獲得 raw 內(nèi)容

例如在常用的 post 請求中,Content-Typeapplication/json 時就可用如下方法獲取 json 請求體內(nèi)容。

$content = $request->getBody()->__toString();
$raw_array = json_decode($content, true);

獲得頭部

$header = $request->getHeaders();

獲得 server

$server = $request->getServerParams();

獲得 cookie

$cookie = $request->getCookieParams();

將數(shù)據(jù)掛載到當前請求對象 $request 上

將某個需要的數(shù)據(jù)掛載到當前請求對象 $request 上,方便在本次請求生命周期內(nèi)的后續(xù)其他方法中調(diào)用這個數(shù)據(jù)。

支持鏈式調(diào)用。

$this->request()->withAttribute($key, $value);

// 鏈式調(diào)用
$this->request()->withAttribute($key1, $value1)->withAttribute($key2, $value2);

注意:掛載的數(shù)據(jù)只對每次請求當前請求生命周期有效。

獲取當前請求對象 $request 上的掛載數(shù)據(jù)

// 獲取單個數(shù)據(jù)
$this->request()->getAttribute($key);

// 獲取所有 返回數(shù)組
$this->request()->getAttributes(); // array

注意:掛載的數(shù)據(jù)只對每次請求當前請求生命周期有效。

丟棄掛載到當前請求對象 $request 上的某個數(shù)據(jù)

$this->request()->withoutAttribute($uselessKey);

使用示例:

<?php

namespace App\HttpController;

use EasySwoole\Http\AbstractInterface\Controller;

class Index extends Controller
{
    public function onRequest(?string $action): ?bool
    {
        // 掛載數(shù)據(jù) 到 $request 對象上
        $this->request()->withAttribute('name', 'easyswoole')
            ->withAttribute('age', 18);

        return parent::onRequest($action); // TODO: Change the autogenerated stub
    }

    public function index()
    {
        // 獲取掛載的單個數(shù)據(jù)
        $ret = $this->request()->getAttribute('name'); // 'easyswoole'

        // 獲取掛載的全部數(shù)據(jù)
        $rets = $this->request()->getAttributes();

        // 刪除某個掛載數(shù)據(jù)
        $this->request()->withoutAttribute('name');

        // 再次獲取掛載的全部數(shù)據(jù)
        $rets1 = $this->request()->getAttributes();

        var_dump($ret, $rets, $rets1);

        // 運行結(jié)果:
        /*
        string(10) "easyswoole"
        array(2) {
          ["name"]=>
          string(10) "easyswoole"
          ["age"]=>
          int(18)
        }
        array(1) {
          ["age"]=>
          int(18)
        }
        */
    }
}
主站蜘蛛池模板: www久 | 亚洲一区二区三区精品视频 | 国产欧美精品一区 | 中文字幕亚洲欧美 | 精品少妇一区二区三区日产乱码 | 伊人久久一区二区三区 | 欧美久久一级特黄毛片 | 国偷自产一区二区免费视频 | 亚洲一区 中文字幕 | 北条麻妃一区二区三区在线 | 日本在线观看 | 欧美怡红院视频一区二区三区 | 精品亚洲一区二区三区四区五区 | 日韩资源| 91视频三区 | 成人在线视频免费观看 | 欧美激情在线精品一区二区三区 | 久久久久国 | 欧美电影一区 | 精品国产仑片一区二区三区 | 亚洲精品久久久一区二区三区 | 99在线视频精品 | 久久午夜影院 | 国产999精品久久久久久麻豆 | 成人午夜影院 | 欧美亚洲免费 | 久久精品亚洲一区二区 | 成人精品视频99在线观看免费 | 欧美精品成人在线视频 | 久久不射电影网 | 欧美日韩一级在线观看 | 成人不卡在线 | 成人午夜在线 | 一区二区av | 天天天插 | 日韩成人在线播放 | 爱爱免费视频网站 | 国产一区二区三区四 | 亚洲精品久久一区二区三区 | 日韩在线免费视频 | 欧美日韩精品久久久 |