2016-04-26 72 views
10

我們已經知道如何在斯利姆3添加自定義404 notFoundHandler如何在Slim 3中手動啓動404處理程序?

$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'); 
    }; 
}; 

我想在我的路線之一,手動觸發此。

在Slim 2中,我們能夠做些類似於$app->notFound()的事情。 Slim 3中的等價物是什麼?

回答

17

您需要拋出\修身\異常\ NotFoundException的新實例

throw new \Slim\Exception\NotFoundException($request, $response);