Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I manually initiate the 404 handler in Slim 3?

We already know how to add a custom 404 notFoundHandler in Slim 3:

$container['notFoundHandler'] = function ($c) {
    return function ($request, $response) use ($c) {
        return $c->view->render($response, 'pages/404.html.twig') 
            ->withStatus(404)
            ->withHeader('Content-Type', 'text/html');
    };
};

I would like to trigger this manually in one of my routes.

In Slim 2, we were able to do something like $app->notFound(). What is the equivalent in Slim 3?

like image 441
alexw Avatar asked Apr 26 '16 16:04

alexw


Video Answer


1 Answers

You need to throw a new instance of \Slim\Exception\NotFoundException

throw new \Slim\Exception\NotFoundException($request, $response);
like image 85
Ben Swinburne Avatar answered Sep 22 '22 13:09

Ben Swinburne