合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
# 输出缓冲中间件 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(); ~~~