output - kouji6309/SingleMVC GitHub Wiki
輸出資料至緩衝區。SingleMVC::output 的別名。
此函數通常用來做最後的輸出,若是要在 View 中印出變數,請使用原生 echo 函數
output(string $view, mixed $data = [], mixed $flag = false) : mixed
-
輸出種類。輸出前設定對應的
Content-Type
。若view
包含.
則以此為檔名下載檔案。
有效值: View 名稱 或json
、html
、text
、jpeg
、png
或 完整檔名。 -
輸出的資料。若為陣列或物件且
view
為 View 名稱將會展開成員。 -
附加選項。
- 布林:是否傳回並清除輸出的內容
- 整數:指定 HTTP 回應碼
- 字串:輸出至緩衝區的內容。
-
null
:不傳回。
輸出指定的 View
output('header', ['title' => 'Hello World']);
output('body');
output('footer');
輸出純文字
output('text', 'OK');
輸出 HTML
output('html', '<div class="red">OK</div>');
輸出 JSON (REST 錯誤訊息)
output('json', ['message' => 'not found'], 404);
輸出圖片
output('png', imagecreatefrompng(ROOT.DS.'images'.DS.'logo.png'));
取得 JSON
$json = output('json', ['key' => 'value'], true);
下載檔案
output('foo.zip', file_get_contents(ROOT.DS.'files'.DS.'foo.zip'));