How to use PSR-7 middlewares with Slim

I’ve found this treasure trove of middlewares: https://github.com/oscarotero/psr7-middlewares

I’m interested in using a few of these, like

Middleware::trailingSlash()
Middleware::minify()
Middleware::languageNegotiator(['gl', 'es', 'en'])

The usage example give on this page do the following:

$relay = new RelayBuilder();
$dispatcher = $relay->newInstance([ 
    ... middlewares are added here ...
]);
$response = $dispatcher(ServerRequestFactory::fromGlobals(), new Response());

I’ve looked through slim and I was unable to find a RelayBuilder class. Dispatcher is part of fast-route, so I can probably figure that out but I’m stumped by the RelayBuilder reference.

So my question is: how would I implement this type of functionality with Slim?

Thanks once again.

In Slim, you simply call the add() method to add new Middleware to the stack:

$app->add(Middleware::trailingSlash());

Excellent. I didn’t dare hope it would be this simple. Thank you.

Allow me to bump this one more time. The docs on this middleware page give an example like to to create a stream factory:

Middleware::setStreamFactory(function ($file, $mode) {
    return new Stream($file, $mode);
});

which I’ve altered to read like this:

Psr7Middlewares\Middleware::setStreamFactory(function ($file, $mode) {
    return new \Slim\Http\Stream($file, $mode);
});

However, I’m getting the following error when this stream factory is being used by the middleware:

Oops! Slim\Http\Stream::attach argument must be a valid PHP resource

Does anybody know how to properly hook this up with/to Slim?

Thanks in advance.

Sorry for the ping, but does anybody know how to properly resolve this?

why you need streamfactory?
simply do as akrabat wrote.

$app->add(Psr7Middlewares\Middleware::trailingSlash());
in Middleware.php work fine

If I do that, it complains about a missing stream factory.

i add one line as above and trailingSlash() work without problem.
show your code example.

edit: which Slim version you use?