[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');
}
}
~~~
- 序言
- 更新日志
- 安裝
- 規范
- 常量
- 配置
- 自動加載
- MVC
- 模塊
- 控制器
- 模型
- 視圖
- php原生模板
- 模板引擎
- 變量輸出
- 模板注釋
- 模板繼承
- 模板引用
- 流程控制
- 原樣輸出
- 服務組件
- Hook組件
- Request組件
- Router組件
- Cookie組件
- Encrypter組件
- Dispatch組件
- Response組件
- View組件
- Session組件
- Helper組件
- 數據分頁
- 數據驗證
- Logger組件
- Cache組件
- Redis組件
- Connection組件
- 執行sql語句
- 查詢生成器
- 查詢方法詳解
- Schema
- Captcha組件
- CLI
- CLI工具
- 事件
- 類事件
- 實例事件
- 全局事件
- 助手函數
- 擴展
- 異常
- 部署
- Apache
- Nginx
- IIS
- 虛擬主機