ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
[TOC] ### 组件说明 > Lying的Response是内置组件,用于控制响应用户操作。 ### 配置选项 | 配置名 | 参数类型 | 可选 | 默认值 | 说明 | | --- | --- | --- | --- | --- | | class | string | 是 | lying\service\Response | 不可更改 | ### 示例配置 Response组件暂时不需要配置,可直接使用 ### 调用方式 ~~~php \Lying::$maker->get('response'); \Lying::$maker->response(); \Lying::$maker->dispatch; ~~~ ### 方法列表 ~~~php /** * 重置状态 * @return $this */ public function clear(); ~~~ ***** ~~~php /** * 设置要发送的头 * @param string|array $name header名,如果此参数是一个数组,则会判断为一个header数组,value无效 * @param string $value header值 * @return $this */ public function setHeader($name, $value = null); ~~~ ***** ~~~php /** * 设置HTTP状态码 * @param int $code * @return $this */ public function setStatusCode($code); ~~~ ***** ~~~ /** * 设置要发送的数据 * @param mixed $data 要发送的数据 * @return $this */ public function setContent($data); ~~~ * * * * * ~~~php /** * 发送响应(只有在调用这个方法的时候才是真正的发送) */ public function send(); ~~~ * * * * * ~~~php /** * 发送文件 * @param string $filePath 文件的路径 * @param string $attachmentName 要保存的文件名,如果没写则使用原来的的文件名 * @param string $mime 文件的mime,不写的话根据文件后缀去判断,否则就是application/octet-stream * @param bool $inline 文件的输出类型是否是inline,否则是attachment * @return $this */ public function sendFile($filePath, $attachmentName = null, $mime = null, $inline = false); ~~~ * * * * * ~~~php /** * 发送字符串当做文件内容 * @param string $content 文件内容 * @param string $attachmentName 要保存的文件名 * @param string $mime 文件的mime,不写的话就是application/octet-stream * @param bool $inline 文件的输出类型是否是inline,否则是attachment * @return $this */ public function sendContentAsFile($content, $attachmentName, $mime = null, $inline = false); ~~~ * * * * * ~~~php /** * 使用X-Sendfile下载文件 * @param string $filePath 文件路径 * @param string $attachmentName 要保存的文件名 * @param string $mime 文件的mime,不写的话根据文件后缀去判断,否则就是application/octet-stream * @param bool $inline 文件的输出类型是否是inline,否则是attachment * @param string $xHeader 使用的HTTP头,默认为nginx的X-Accel-Redirect,apache用X-Sendfile * @return $this */ public function xSendFile($filePath, $attachmentName = null, $mime = null, $inline = false, $xHeader = 'X-Accel-Redirect'); ~~~ * * * * * ~~~php /** * HTTP重定向 * ``` * redirect('get', ['id' => 100]);跳转到[当前模块/当前控制器/get] * redirect('admin/post', ['id' => 100]);跳转到[当前模块/admin/post] * redirect('lying/index/name', ['id' => 100]);跳转到[lying/index/name],参见URL生成 * redirect('/admin/post', ['id' => 100]);跳转到[/admin/post/?id=100] * redirect('https://www.baidu.com') 必须带协议头,跳转到百度 * ``` * @param string $url 重定向的地址 * @param array $params URL参数 * @param bool $normal 是否把参数设置成?a=1&b=2,默认否,优先pathinfo,只对非/开头的path * @param int $statusCode HTTP状态码,默认302,如果ajax请求出错,可以手动设置200 * @return $this */ public function redirect($url, array $params = [], $normal = false, $statusCode = 302); ~~~ ### 使用示例 ~~~php <?php namespace module\index\controller; use lying\service\Controller; /** * Class IndexCtrl * @package module\index\controller */ class IndexCtrl extends Controller { /** * 首页 * @return string */ public function index() { //常规的页面渲染输出方式 //return $this->render(); //更多的输出方式 $response = $this->maker->response; //输出渲染后的页面 //return $response->setContent($this->render()); //设置输出的http返回码 //$response->setStatusCode(200); //立即发送,立即发送后,如果再次调用send,则不会发送,除非调用clear清空所有输出缓冲 //$response->setContent($this->render())->send(); //输出文件 //return $response->sendFile('/www/file/a.txt'); //把字符串当做文件输出 //return $response->sendContentAsFile('文件内容', 'hello.txt'); //调用nginx的sendfile文件下载 //return $response->xSendFile('/www/data/test.mp3'); } } ~~~