输出缓冲中间件让你可以在两种输出缓冲模式之间切换:APPEND
(默认)和 PREPEND
模式。APPEND
模式将会使用现有的响应正文来追加内容。PREPEND
模式将会创建一个新的响应正文对象并将内容前置到来自现有响应正文的输出。此中间件应放在中间件堆栈的中心,以使其在最后执行。
<?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();
/**
* 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();