title: How to implement this Slim 3 middleware to Slim 4 middleware
body: <p>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</p>
  <pre><code>//route middleware
  $app-&gt;add(function (Request $request, Response $response, callable $next) {
  
     $public = array(
        &quot;ping&quot;, 
        &quot;guest&quot;, 
        &quot;login&quot;,
        &quot;api-login&quot;, 
        &quot;logout&quot;
     );
     
     $route = $request-&gt;getAttribute('route');
  
     // return NotFound for non existent route
     if (empty($route)) {
        throw new NotFoundException($request, $response);
     }
  
     $name = $route-&gt;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-&gt;withRedirect('/login'); 
        }
     }
  
     return $next($request, $response);
  });
  </code></pre>