I have this code from my Slim-3 middleware, how can I convert this to Slim-4 middleware. It seem in slim4, we need to return some response instead return the callable $next
//route middleware
$app->add(function (Request $request, Response $response, callable $next) {
$public = array(
"ping",
"guest",
"login",
"api-login",
"logout"
);
$route = $request->getAttribute('route');
// return NotFound for non existent route
if (empty($route)) {
throw new NotFoundException($request, $response);
}
$name = $route->getName();
//if route is not public, then
// - get bearer token from authorization header / httponly cookie
// - validate jwt token
if (!in_array($name, $public)) {
//get token status from cookie token
$tokenStatus = checkTokenStatus();
if (!$tokenStatus) {
//redirect to guest page
return $response->withRedirect('/login');
}
}
return $next($request, $response);
});
According to the documentation:
You need to change the function arguments to:
function(Request $request, RequestHandler $handler)
and then return the response by:
$response = $handler->handle($request);
return $response;