Request 对象

接收客户端的 HTTP 请求对象

生命周期

Request 对象在系统中以单例模式存在,自收到客户端 HTTP 请求时自动创建,直至请求结束自动销毁。Request 对象完全符合 PSR-7 中的所有规范。

核心方法

getRequestParam()

用于获取用户通过 POST 或者 GET 提交的参数(注意:若 POSTGET 存在同键名参数,则以POST 为准)。

示例:

// 在控制器中 可以通过 $this->request() 获取到 Request 对象
// $request = $this->request();

// 获取 `POST` 或者 `GET` 提交的所有参数
$data = $request->getRequestParam();
var_dump($data);

// 获取 `POST` 或者 `GET` 提交的单个参数
$orderId = $request->getRequestParam('orderId');
var_dump($orderId);

// 获取 `POST` 或者 `GET` 提交的多个参数
$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 对象的数组
$data = $request->getUploadedFiles();
var_dump($data);

点击查看 UploadFile对象

getBody()

获取以非 form-datax-www-form-urlenceded 编码格式 POST 提交的原始数据,相当于PHP中的 $HTTP_RAW_POST_DATA

获得 get 内容

$get = $request->getQueryParams();

获得 post 内容

$post = $request->getParsedBody();

获得 raw 内容

例如在常用的 post 请求中,Content-Typeapplication/json 时就可用如下方法获取 json 请求体内容。

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

获得头部

$header = $request->getHeaders();

获得 server

$server = $request->getServerParams();

获得 cookie

$cookie = $request->getCookieParams();

将数据挂载到当前请求对象 $request 上

将某个需要的数据挂载到当前请求对象 $request 上,方便在本次请求生命周期内的后续其他方法中调用这个数据。

支持链式调用。

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

// 链式调用
$this->request()->withAttribute($key1, $value1)->withAttribute($key2, $value2);

注意:挂载的数据只对每次请求当前请求生命周期有效。

获取当前请求对象 $request 上的挂载数据

// 获取单个数据
$this->request()->getAttribute($key);

// 获取所有 返回数组
$this->request()->getAttributes(); // array

注意:挂载的数据只对每次请求当前请求生命周期有效。

丢弃挂载到当前请求对象 $request 上的某个数据

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

使用示例:

<?php

namespace App\HttpController;

use EasySwoole\Http\AbstractInterface\Controller;

class Index extends Controller
{
    public function onRequest(?string $action): ?bool
    {
        // 挂载数据 到 $request 对象上
        $this->request()->withAttribute('name', 'easyswoole')
            ->withAttribute('age', 18);

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

    public function index()
    {
        // 获取挂载的单个数据
        $ret = $this->request()->getAttribute('name'); // 'easyswoole'

        // 获取挂载的全部数据
        $rets = $this->request()->getAttributes();

        // 删除某个挂载数据
        $this->request()->withoutAttribute('name');

        // 再次获取挂载的全部数据
        $rets1 = $this->request()->getAttributes();

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

        // 运行结果:
        /*
        string(10) "easyswoole"
        array(2) {
          ["name"]=>
          string(10) "easyswoole"
          ["age"]=>
          int(18)
        }
        array(1) {
          ["age"]=>
          int(18)
        }
        */
    }
}