💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
## 响应 响应组件用来将控制器返回的数据、设置的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/tree/v2.1/app/Web/Controllers/IndexController.php#L17) ## PSR 定义方法 | 方法 | 描述 | | --- | --- | | withHeader($name, $value) | 设置header | | withAddedHeader($name, $value) | header追加值 | | withoutHeader($name) | 移除header | | withBody(StreamInterface $body) | 设置主体 | | withStatus($code, $reasonPhrase = '') | 设置状态码 | - Sample 1 ``` $body = (new StreamFactory())->createStream($content); $response ->withContentType('text/html', 'utf-8') ->withBody($body); ``` ## 特有方法 | 方法 | 描述 | | --- | --- | | withCookies(array $cookies) | 批量设置cookie | | withCookie(Cookie $cookie) | 设置cookie | | withContentType(string $type, string $charset = '') | 设置ContentType | | redirect(string $url, int $status = 302) | 重定向 | | end() | 发送结果,并结束请求处理 | - Sample 1 ``` $cookie = (new CookieFactory())->createCookie($name, $value, $expire); $response->withCookie($cookie); ```