## 响应 响应组件用来将控制器返回的数据、设置的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` 的 `Mix\Http\Server\HttpServer::handle` 方法的闭包参数中获取。 [>> StartCommand::class 源码 <<](https://github.com/mix-php/mix-skeleton/blob/master/app/Http/Commands/StartCommand.php#L84) ## 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); ```