输出缓冲中间件使您能够在两种输出缓冲模式之间切换:(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();