在slim框架中返回/调用NotFoundHandler

时间:2016-06-21 22:10:23

标签: php slim

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

目前我在做

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

但是这不会重定向到我的“普通”或应用程序的标准404页面。

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

2 个答案:

答案 0 :(得分:0)

你可以抛出NotFoundException

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

答案 1 :(得分:0)

我找不到处理程序

<?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;
    };
};