How to return 404 page error in slim v4

http_response_code(404);
die();

how to use this code in slim v4 to make it works?

$app->get('/hello/{name}', function (Request $request, Response $response, $args) {
    $name = $args['name'];
   if($name=="world"){
      $response->getBody()->write("Hello, $name");
   }else{
     http_response_code(404);
     die();
   }
    return $response;
});

You can do that by throwing an HttpNotFoundException.

use Slim\Exception\HttpNotFoundException;
// ...

throw new HttpNotFoundException($request);
1 Like