# Prolem with Middleware

**URL:** <https://discourse.slimframework.com/t/prolem-with-middleware/5846>\
**Category:** Questions\
**Created:** [March 15, 2024, 9:27pm UTC](https://discourse.slimframework.com/t/prolem-with-middleware/5846 "2024-03-15T21:27:05Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![tavares.matheus](https://avatars.discourse-cdn.com/v4/letter/t/65b543/32.png) [@tavares.matheus](https://discourse.slimframework.com/u/tavares.matheus)\
**Post date:** [March 15, 2024, 9:27pm UTC](https://discourse.slimframework.com/t/prolem-with-middleware/5846/1 "2024-03-15T21:27:05Z")

</div>

Hey there guys, I have this middleware:

```auto
<?php

declare(strict_types=1);

namespace Masoretic\Middlewares;

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;

class ContentTypeJsonMiddleware
{
    public function __invoke(
        Request $request,
        RequestHandler $handler
    ): Response
    {
        header('Content-Type: application/json');
        $response = $handler->handle($request);
        $response = new Response();
        return $response;
    }
}

```

But, I’m receiving this error message:  
“Cannot instantiate interface Psr\Http\Message\ResponseInterface”  
Any one knows why?

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [March 16, 2024, 10:01am UTC](https://discourse.slimframework.com/t/prolem-with-middleware/5846/2 "2024-03-16T10:01:51Z")

</div>

> use Psr\Http\Message\ResponseInterface as Response;  
> $response = new Response();

It is not possible to create an instance from an interface.

To fix this, either create an Response from your PSR-7 package (slim or nyholm)  
or use the ResponseFactory.

> header(‘Content-Type: application/json’);

This (header) function should not be used in a PSR-7 application. To change the response header, better use the the response object instead.

Example:

```php
$response = $response->withHeader('Content-Type', 'application/json');

```

---

<div class="post-metadata">

**Author:** ![tavares.matheus](https://avatars.discourse-cdn.com/v4/letter/t/65b543/32.png) [@tavares.matheus](https://discourse.slimframework.com/u/tavares.matheus)\
**Post date:** [March 16, 2024, 2:30pm UTC](https://discourse.slimframework.com/t/prolem-with-middleware/5846/3 "2024-03-16T14:30:46Z")

</div>

Hi odan! Thanks for your answer, could you give me a example code on how to create from the ResponseFactory or how to create from Response PSR-7/Slim, please? I’ve tried to findo some examples but couldn’t find it.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![tavares.matheus](https://avatars.discourse-cdn.com/v4/letter/t/65b543/32.png) [@tavares.matheus](https://discourse.slimframework.com/u/tavares.matheus)\
**Post date:** [March 16, 2024, 2:38pm UTC](https://discourse.slimframework.com/t/prolem-with-middleware/5846/4 "2024-03-16T14:38:40Z")

</div>

Just an update, I’ve tried to do this:

```auto
<?php

declare(strict_types=1);

namespace Masoretic\Middlewares;

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;

class ContentTypeJsonMiddleware
{
    public function __invoke(
        Request $request,
        RequestHandler $handler
    ): Response
    {
        header('Content-Type: application/json');
        $response = $handler->handle($request);
        $responseFactory = new \Slim\Psr7\ResponseFactory;
        $response = $responseFactory->createResponse();
        return $response;
    }
}

```

But it returned this: Class "Slim\Psr7\ResponseFactory" not found.  
I cant import this class because my application simply cant find it to import.

---

<div class="post-metadata">

**Author:** ![tavares.matheus](https://avatars.discourse-cdn.com/v4/letter/t/65b543/32.png) [@tavares.matheus](https://discourse.slimframework.com/u/tavares.matheus)\
**Post date:** [March 16, 2024, 2:42pm UTC](https://discourse.slimframework.com/t/prolem-with-middleware/5846/5 "2024-03-16T14:42:13Z")

</div>

Okay, found the solution, I was importing in an old way, if you change the:  
new \Slim\Psr7\ResponseFactory  
to:  
new \Slim\Psr7\Factory\ResponseFactory  
It will 100% work! Thanks Odan 🙂

---

<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:** [March 18, 2024, 7:40am UTC](https://discourse.slimframework.com/t/prolem-with-middleware/5846/6 "2024-03-18T07:40:27Z")

</div>

> [@tavares.matheus](#):
>
> ```auto
> $response = $handler->handle($request);
> $responseFactory = new \Slim\Psr7\ResponseFactory;
> $response = $responseFactory->createResponse();
> return $response;
> 
> ```

Note, that you have a `$response` returned to you from `handle()`, so do not need to create a new one.

It’s relatively rare for middleware to not return that `$response` instance from `handle()` as it represents a lot of processing that’s happened in other middleware and in the route handler itself.

---

<div class="post-metadata">

**Author:** ![tavares.matheus](https://avatars.discourse-cdn.com/v4/letter/t/65b543/32.png) [@tavares.matheus](https://discourse.slimframework.com/u/tavares.matheus)\
**Post date:** [March 18, 2024, 7:33pm UTC](https://discourse.slimframework.com/t/prolem-with-middleware/5846/7 "2024-03-18T19:33:09Z")

</div>

Thanks for your advice akrabat! I’ve removed the response creation and used only the handle instead, it worked smoothly!

---

<div class="post-metadata">

**Author:** ![ghabriel](https://avatars.discourse-cdn.com/v4/letter/g/bb73d2/32.png) [@ghabriel](https://discourse.slimframework.com/u/ghabriel)\
**Post date:** [April 3, 2024, 3:55am UTC](https://discourse.slimframework.com/t/prolem-with-middleware/5846/8 "2024-04-03T03:55:36Z")

</div>

Just a suggestion as [PSR-15: HTTP Server Request Handlers - PHP-FIG](https://www.php-fig.org/psr/psr-15/) mentioned

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/slimframework/original/2X/b/b77971a07e27a1ca044863c2fe66148939f4d5b1.png)  
If you’re in a company. Maybe it would be better to use some standard.  
Like this example

```auto
<?php

declare(strict_types=1);

namespace Masoretic\Middlewares;

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
use Psr\Http\Server\MiddlewareInterface;

final class ContentTypeJsonMiddleware implements MiddlewareInterface
{
    public function process(Request $request, RequestHandler $handler): Response
    {
        header('Content-Type: application/json');
        
        return $handler->handle($request);
    }
}

```

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [April 3, 2024, 6:01pm UTC](https://discourse.slimframework.com/t/prolem-with-middleware/5846/9 "2024-04-03T18:01:41Z")

</div>

> [@ghabriel](#):
>
> ```auto
> header('Content-Type: application/json');
> 
> ```

Note: While this approach might work in some scenarios, it’s more robust to PSR-7 standards to use the Response object provided by the PSR interfaces for modifying headers.
