output - kouji6309/SingleMVC GitHub Wiki

說明

輸出資料至緩衝區。SingleMVC::output 的別名。
此函數通常用來做最後的輸出,若是要在 View 中印出變數,請使用原生 echo 函數


原型

output(string $view, mixed $data = [], mixed $flag = false) : mixed

參數

  • view

    輸出種類。輸出前設定對應的 Content-Type。若 view 包含 . 則以此為檔名下載檔案。
    有效值: View 名稱 或 jsonhtmltextjpegpng 或 完整檔名。

  • data

    輸出的資料。若為陣列或物件且 view 為 View 名稱將會展開成員。

  • flag

    附加選項。

    • 布林:是否傳回並清除輸出的內容
    • 整數:指定 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'));
⚠️ **GitHub.com Fallback** ⚠️