How to get OutputBufferingMiddleware to work. (Slim 4)

What is the proper way to use OutputBufferingMiddleware with Slim 4?
I tried using both snippets I found in the docs, but neither one of them appears to be working.

It appears I need to pass a StreamFactory to the middleware, but I can’t figure out where to get one from.

Hi @ruben_vreeken

Just take a look at the documentation, example:

// ...
use Slim\Psr7\Factory\StreamFactory;

// ...
$streamFactory = new StreamFactory();

PS: This example requires slim/psr7 as PSR 7 library:

composer require slim/psr7

The update guide’s snippet is missing the StreamFactory.

In my case I was using the Nyholm PSR7, mostly because according to the Nyholm PSR7 readme, Slim’s psr 7 implementation didn’t pass all integration tests while Nyholm did (though I suppose that information could be outdated by now).

Anyways, I was under the impression that Slim intended to abstract away the differences between different supported PSR7 implementations. Do I understand correctly that Slim doesn’t expose some utility to easily get a StreamFactory regardless of which of the supported PSR7 implementations are in use? I’m new to this PSR7 thing, so I’m not yet sure if it makes much sense to expect such a feature or not.

Anyways, I’ll go and figure out how to get at Nyholm’s stream factory.

Slim’s psr 7 implementation didn’t pass all integration tests

According to this, all Slim tests are passing: https://github.com/php-http/psr7-integration-tests

Anyways, I was under the impression that Slim intended to abstract away the differences between different supported PSR7 implementations

That’s correct. In Slim 4 the PSR-7 implementation is decoupled from the App core. This means you can also install other PSR-7 implementations like Nyholm.

Do I understand correctly that Slim doesn’t expose some utility to easily get a StreamFactory regardless of which of the supported PSR7 implementations are in use?

It’s not that difficult. Just take a look at the examples in the documentation: https://github.com/Nyholm/psr7

Example:

$psr17Factory = new \Nyholm\Psr7\Factory\Psr17Factory();

You’re right, once things are figured out it’s not difficult at all.

Wrapping your head around PSR7, 15, 17, Slim and Nyholm/psr7 at the same time while being new to all of them can be a bit of a puzzle though.

P.S: Good to know Slim is passing all PSR7 integration tests now.