Return/invoke slim框架中的NotFoundHandler

Return/invoke the NotFoundHandler in slim framework

我正在尝试重定向到 404 页面。 Laravel 可以选择 abort(404)

目前我在做

return $response->withStatus(404)->withHeader('Content-Type', 'text/html')->write('Page not found');

但这不会重定向到我的 "normal" 或应用程序的标准 404 页面。

我已阅读 docs,但如何从应用程序中的任何位置调用 notFoundHandler?

你可以投NotFoundException.

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

我没有找到处理程序

<?php

$app = new \Slim\App();

$container = $app->getContainer();

$container['notFoundHandler'] = function ($c) {
    return function (\Slim\Http\Request $request, \Slim\Http\Response $response) use ($c) {
        $view = $c->get('view');
        $view->render($response, '404.php');
        return $response;
    };
};