# How to use PSR-7 middlewares with Slim

**URL:** https://discourse.slimframework.com/t/how-to-use-psr-7-middlewares-with-slim/717
**Category:** Questions
**Created:** [September 15, 2016, 12:38am UTC](https://discourse.slimframework.com/t/how-to-use-psr-7-middlewares-with-slim/717 "2016-09-15T00:38:24Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![TheLobos](https://avatars.discourse-cdn.com/v4/letter/t/e9bcb4/32.png) [@TheLobos](https://discourse.slimframework.com/u/TheLobos)
#### Post date: [September 15, 2016, 12:38am UTC](https://discourse.slimframework.com/t/how-to-use-psr-7-middlewares-with-slim/717/1 "2016-09-15T00:38:24Z")

</div>

I’ve found this treasure trove of middlewares: [https://github.com/oscarotero/psr7-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.

---

<div class="post-metadata">

### Author: ![akrabat](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/akrabat/32/9_2.png) [@akrabat](https://discourse.slimframework.com/u/akrabat)
#### Post date: [September 15, 2016, 7:21am UTC](https://discourse.slimframework.com/t/how-to-use-psr-7-middlewares-with-slim/717/2 "2016-09-15T07:21:30Z")

</div>

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

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

```

---

<div class="post-metadata">

### Author: ![TheLobos](https://avatars.discourse-cdn.com/v4/letter/t/e9bcb4/32.png) [@TheLobos](https://discourse.slimframework.com/u/TheLobos)
#### Post date: [September 15, 2016, 10:24am UTC](https://discourse.slimframework.com/t/how-to-use-psr-7-middlewares-with-slim/717/3 "2016-09-15T10:24:08Z")

</div>

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

---

<div class="post-metadata">

### Author: ![TheLobos](https://avatars.discourse-cdn.com/v4/letter/t/e9bcb4/32.png) [@TheLobos](https://discourse.slimframework.com/u/TheLobos)
#### Post date: [September 16, 2016, 12:10am UTC](https://discourse.slimframework.com/t/how-to-use-psr-7-middlewares-with-slim/717/4 "2016-09-16T00:10:00Z")

</div>

> [@akrabat](#):
>
> $app-\>add(Middleware::trailingSlash());

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.

---

<div class="post-metadata">

### Author: ![TheLobos](https://avatars.discourse-cdn.com/v4/letter/t/e9bcb4/32.png) [@TheLobos](https://discourse.slimframework.com/u/TheLobos)
#### Post date: [September 17, 2016, 5:54pm UTC](https://discourse.slimframework.com/t/how-to-use-psr-7-middlewares-with-slim/717/5 "2016-09-17T17:54:20Z")

</div>

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

---

<div class="post-metadata">

### Author: ![1f7](https://avatars.discourse-cdn.com/v4/letter/1/91b2a8/32.png) [@1f7](https://discourse.slimframework.com/u/1f7)
#### Post date: [September 17, 2016, 10:11pm UTC](https://discourse.slimframework.com/t/how-to-use-psr-7-middlewares-with-slim/717/6 "2016-09-17T22:11:26Z")

</div>

why you need streamfactory?  
simply do as akrabat wrote.

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

---

<div class="post-metadata">

### Author: ![TheLobos](https://avatars.discourse-cdn.com/v4/letter/t/e9bcb4/32.png) [@TheLobos](https://discourse.slimframework.com/u/TheLobos)
#### Post date: [September 17, 2016, 11:37pm UTC](https://discourse.slimframework.com/t/how-to-use-psr-7-middlewares-with-slim/717/7 "2016-09-17T23:37:56Z")

</div>

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

---

<div class="post-metadata">

### Author: ![1f7](https://avatars.discourse-cdn.com/v4/letter/1/91b2a8/32.png) [@1f7](https://discourse.slimframework.com/u/1f7)
#### Post date: [September 17, 2016, 11:48pm UTC](https://discourse.slimframework.com/t/how-to-use-psr-7-middlewares-with-slim/717/8 "2016-09-17T23:48:02Z")

</div>

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

edit: which Slim version you use?
