通知短信+运营短信,5秒速达,支持群发助手一键发送🚀高效触达和通知客户 广告
## 请求 请求组件用来获取所有 HTTP 请求参数,基于 [PSR-7](https://www.php-fig.org/psr/psr-7/) / [PSR-17](https://www.php-fig.org/psr/psr-17/) 标准实现。 ## 组件 使用 [composer]([https://www.phpcomposer.com/](https://www.phpcomposer.com/)) 安装: ~~~ composer require mix/http-message ~~~ ## 来源 请求类定义于 `mix/http-message` 库,由 `mix/http-server` 的 `HandlerInterface::handleHTTP` 方法中获取,最终会传递到控制器的构造方法与调用方法中。 - [Router.php#L210](https://github.com/mix-php/route/blob/master/src/Router.php#L210) - [IndexController.php#L17](https://github.com/mix-php/mix-skeleton/blob/master/app/Web/Controllers/IndexController.php#L17) ## PSR 定义方法 | 方法 | 描述 | | --- | --- | | getContext() : \Mix\Context\Context | 获取上下文 | | getProtocolVersion() : string | 获取协议版本 | | getHeaders() : array | 获取全部header | | hasHeader($name) : bool | 判断 header 是否存在 | | getHeader($name) : array | 获取 header (http协议header是可以同名的) | | getHeaderLine($name) : string | 获取 header 单行 (逗号分隔) | | getBody() : StreamInterface | 获取主体原始内容 | | getMethod() : string | 获取请求方法 | | getUri() : UriInterface | 获取请求uri | | getSwooleRequest() : \Swoole\Http\Request | 获取 swoole 请求对象 | | getServerParams() : array | 获取服务器参数 | | getCookieParams() : array | 获取Cookie| | getQueryParams() : array | 获取请求参数 (GET) | | getUploadedFiles() : UploadedFileInterface[] | 获取上传文件 | | getParsedBody() : null \| array | 获取解析主体 (POST) | | getAttributes() : array | 获取全部属性 (GET/POST/Cookie) | | getAttribute($name, $default = null) | 获取属性 | ## Context 请求上下文 Web 开发中经常会有一些数据会需要贯穿整个请求中,这些数据可以保存到 Context 中,比如:中间件中获取用户的 member_id 保存到上下文中,控制器中取出 member_id 并处理。 - 获取上下文对象 ~~~ $context = $request->getContext(); ~~~ - 保存数据 ~~~ $context->withValue('key', $value); ~~~ - 获取数据 ~~~ $value = $context->value('key'); ~~~