【问题标题】:How do I manually initiate the 404 handler in Slim 3?如何在 Slim 3 中手动启动 404 处理程序?
【发布时间】:2016-08-20 14:29:01
【问题描述】:

我们已经知道如何在 Slim 3 中添加自定义 404 notFoundHandler

$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 中的等价物是什么?

【问题讨论】:

    标签: php httpresponse slim slim-3


    【解决方案1】:

    你需要抛出一个新的 \Slim\Exception\NotFoundException 实例

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

    【讨论】:

    • 我在谷歌上找到了这个答案,所以我正在为新用户发表评论。对于 Slim 4,使用 HttpNotFoundException,例如:throw new \Slim\Exception\HttpNotFoundException($request, "Resource not found")
    • 你如何使用第二个参数?说在错误页面上显示。
    猜你喜欢
    • 2017-04-09
    • 1970-01-01
    • 1970-01-01
    • 2016-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-27
    • 2015-05-12
    相关资源
    最近更新 更多