# Upgrade v3 to v4 : how should I adapt my middleware?

**URL:** <https://discourse.slimframework.com/t/upgrade-v3-to-v4-how-should-i-adapt-my-middleware/3626>\
**Category:** Questions\
**Created:** [November 7, 2019, 10:15pm UTC](https://discourse.slimframework.com/t/upgrade-v3-to-v4-how-should-i-adapt-my-middleware/3626 "2019-11-07T22:15:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![paquerette](https://avatars.discourse-cdn.com/v4/letter/p/a8b319/32.png) [@paquerette](https://discourse.slimframework.com/u/paquerette)\
**Post date:** [November 7, 2019, 10:15pm UTC](https://discourse.slimframework.com/t/upgrade-v3-to-v4-how-should-i-adapt-my-middleware/3626/1 "2019-11-07T22:15:46Z")

</div>

Hi,

I’m working on migrating my slim v3 (rest backend) to v4.  
The upgrade “guide” is sooo light considering all the changes 😯

MiddleWare have changed of interface.

Any tips to convert from the v3 interface to the v4?

I use middleware to implement authentication:

> <https://github.com/dev-mansonthomas/RedCrossQuest/blob/master/server/src/Middleware/AuthorisationMiddleware.php>

Thanks,  
Thomas.

---

<div class="post-metadata">

**Author:** ![paquerette](https://avatars.discourse-cdn.com/v4/letter/p/a8b319/32.png) [@paquerette](https://discourse.slimframework.com/u/paquerette)\
**Post date:** [November 7, 2019, 11:27pm UTC](https://discourse.slimframework.com/t/upgrade-v3-to-v4-how-should-i-adapt-my-middleware/3626/2 "2019-11-07T23:27:09Z")

</div>

Well, I hope that the following is enough:

```
<?php
require '../../vendor/autoload.php';
use \RedCrossQuest\Middleware\AuthorisationMiddleware;
$app->add( new AuthorisationMiddleware($app) );

```

To

```
<?php
declare(strict_types=1);
require '../../vendor/autoload.php';

use Slim\App;
use \RedCrossQuest\Middleware\AuthorisationMiddleware;

return function (App $app) {
 $app->add(AuthorisationMiddleware::class);
};

```

The middleware must implement Psr\Http\Server\MiddlewareInterface  
And the signature of the main method changed from

`public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)`

To  
` public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface`

On you middleware class :

Add this :

```
use Psr\Http\Server\MiddlewareInterface;

class AuthorisationMiddleware implements MiddlewareInterface

```

Update the \_\_invoke methode to the “process” signature

`return $next($request, $response);`

Should be change to

`return $handler->handle($request)`

For denying a request :

```
$response500 = $response->withStatus(500);
…
return $response500;

```

Change it to :

```
$response500 = (new Response())->withStatus(500);
…
return $response500;
```
