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.
akrabat
September 15, 2016, 7:21am
2
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?
1f7
September 17, 2016, 10:11pm
6
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.
1f7
September 17, 2016, 11:48pm
8
i add one line as above and trailingSlash() work without problem.
show your code example.
edit: which Slim version you use?