# 輸出緩沖中間件
The Output Buffering Middleware enables you to switch between two modes of output buffering:`APPEND`(default) and`PREPEND`mode. The`APPEND`mode will use the existing response body to append the contents. The`PREPEND`mode will create a new response body object and prepend the contents to the output from the existing response body. This middleware should be placed on the center of the middleware stack so it gets executed last.
> 輸出緩沖中間件允許您在輸出緩沖的兩種模式之間進行切換: **APPEND** (默認)和 **prepend** 模式。
>
> `APPEND`模式將使用現有的響應體附加內容。
>
> `prepend` 模式將創建一個新的響應主體對象,并將內容預先添加到現有響應主體的輸出中。
>
> 這個中間件應該放在中間件堆棧的中心,這樣它才會最后執行。
## Usage
~~~php
<?php
use Slim\Factory\AppFactory;
use Slim\Middleware\OutputBufferingMiddleware;
use Slim\Psr7\Factory\StreamFactory;
require __DIR__ . '/../vendor/autoload.php';
$app = AppFactory::create();
$streamFactory = new StreamFactory();
/**
* 有兩種模式
* OutputBufferingMiddleware::APPEND(默認模式)——追加到現有的響應體
* OutputBufferingMiddleware::PREPEND——創建全新的響應體
* The two modes available are
* OutputBufferingMiddleware::APPEND (default mode) - Appends to existing response body
* OutputBufferingMiddleware::PREPEND - Creates entirely new response body
*/
$mode = OutputBufferingMiddleware::APPEND;
$outputBufferingMiddleware = new OutputBufferingMiddleware($streamFactory, $mode);
$app->add($outputBufferingMiddleware);
// ...
$app->run();
~~~
- 開始
- 安裝
- 升級指南
- Web服務器
- 概念
- 生命周期
- PSR 7
- 中間件
- 依賴容器
- 實例 及通知和警告處理
- Request
- 請求方法
- 請求頭信息
- 請求主體
- 上傳的文件
- 請求幫助
- 路由對象
- Response
- 響應狀態
- 響應標頭
- 響應體
- 返回JSON
- 視圖模板
- 路由
- 創建路由
- 路由回調
- 路由策略
- 路線占位符
- 路由名
- 路由組
- 路由中間件
- 路由表達式緩存
- 容器識別解析
- 封裝中間件
- 路由的中間件
- 錯誤處理中間件
- 方法重寫的中間件
- 輸出緩沖中間件
- 內容長度中間件
- 擴展功能
- 以 / 結尾的路由模式
- 獲取當前路由
- 設置CORS
- 使用POST表單上傳文件
- 第三方組件
- slim-session
- auth
- slim-api-skeleton
- dir