Slim 4 return $next($request, $response) vs return $response

Hello, in Slim v3, I can modify $request object inside a Middleware. With Slim v4, I can’t return new $request object to others Middleware, as it only returns $response. How can I do?

Have you tried using the handle method?

$response = $handler->handle($request);

1 Like

I have read documentation but it says: Each middleware MUST return an instance of Psr\Http\Message\ResponseInterface, no $request returned. How can I use Handler to modify $request object and return the modified $request to other Middlewares?

You modify the request as you need to inside your middleware, then pass it on. So:

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    $request = $request->withAttribute('foo', 'bar');

    return $handler->handle($request);
1 Like