PSR-15 Zend Diactoros HTTP response emitter middleware for BitFrame microframework
{ "require": { "designcise/bitframe-diactoros": "^1.0.0" } }
Add the following as a dependency in your project's composer.json
:
"designcise/bitframe-diactoros": "^1.0.0"
use \BitFrame\Message\DiactorosResponseEmitter; require 'vendor/autoload.php'; $app = new \BitFrame\Application; // instantiation with default options $emitter = new DiactorosResponseEmitter;// lazy instantiation with default options $emitter = DiactorosResponseEmitter::class;/* instantiate with custom options * * @param \Zend\Diactoros\Response\EmitterInterface|null $emitter */ $emitter = new DiactorosResponseEmitter($emitter); $app->run([ /* The response emitter must be the first middleware so that it * emits the response from all the middleware that follow. */ $emitter ]);
You can create a new instance of \BitFrame\Message\DiactorosResponseEmitter
simply by using the new
keyword.
You can also do lazy instantiation, if it's supported by your middleware dispatcher, by just providing the class name with its namespace, or simply by appending ::class
to an object instance or class name.
You can specify the following argument when creating a new instance of the object:
\Zend\Diactoros\Response\SapiEmitter
) If null
, the default emitter is used. If using a custom emitter, it must implement \Zend\Diactoros\Response\EmitterInterface
.require 'vendor/autoload.php'; $app = new \BitFrame\Application; $emitter = new \BitFrame\Message\DiactorosResponseEmitter; $emitter->setForceEmit(false); $app->run([ function($request, $response, $next) { $response->getBody()->write('Run & Emit Response'); return $response; } ]); $app->run([ $emitter, function($request, $response, $next) { // output has been emitted previously, so \RuntimeException is thrown $response->getBody()->write('Should result in error'); return $response; } ]);
The normal behavior for Diactoros is that once the output is emitted (either when headers have already been sent or if output is present in the output buffer) an exception would be thrown. We can overcome this limitation by forcing emit which would output the response without sending any additional headers.
setForceEmit
: This option is enabled by default and can be enabled/disabled by calling the setForceEmit
method on the Diactoros object instance.isForceEmit
: You can use isForceEmit
method to check what the value of force emit is for that specific object instance.
If force emit option is set to true
and output is from the same \Bitframe\Application
instance, then emitter should be used once only (either in first addMiddleware()
call or last run([])
call) to avoid duplicates. To avoid this behavior, you can create two (or more) distinct application instances with their own respective http responses.
/** * Zend Diactoros Wrapper (PSR-15 / PSR-7 compatible) * * @author Daniyal Hamid * @copyright Copyright (c) 2017-2018 Daniyal Hamid (https://designcise.com) * * @author Zend Framework * @copyright Copyright (c) 2016-2017 Zend Technologies USA Inc. * * @license MIT License and New BSD License */
Zend Diactoros contains classes implementing the accepted PSR-7 HTTP message interfaces; among various uses, it helps emit the HTTP response.
Diactoros Official GitHubLet us know if you have something to say or add
Latest version 1.0.0 released on Feb 14, 2018