【发布时间】:2015-01-02 09:50:03
【问题描述】:
我正在使用 Laravel 4 框架,我已经定义了一大堆路由,现在我想知道所有未定义的 url,如何将它们路由到 404 页面?
【问题讨论】:
标签: laravel routes http-status-code-404
我正在使用 Laravel 4 框架,我已经定义了一大堆路由,现在我想知道所有未定义的 url,如何将它们路由到 404 页面?
【问题讨论】:
标签: laravel routes http-status-code-404
在 Laravel 5.2 中。什么都不做,在errors文件夹中创建一个文件名404.blade.php,它会自动检测404异常。
【讨论】:
未定义的路由会触发Symfony\Component\HttpKernel\Exception\NotFoundHttpException 异常,您可以使用 App::error() 方法在 app/start/global.php 中处理该异常,如下所示:
/**
* 404 Errors
*/
App::error(function(\Symfony\Component\HttpKernel\Exception\NotFoundHttpException $exception, $code)
{
// handle the exception and show view or redirect to a diff route
return View::make('errors.404');
});
【讨论】:
处理错误的推荐方法可以在 Laravel 文档中找到:
http://laravel.com/docs/4.2/errors#handling-404-errors
使用start/global.php文件中的App::missing()函数如下:
App::missing(function($exception)
{
return Response::view('errors.missing', array(), 404);
});
【讨论】:
您可以在以下位置添加一个文件:resources/views/errors/ 调用 404.blade.php 并使用您要在 404 错误时显示的信息。
【讨论】:
我已将我的 laravel 4 代码库升级到 Laravel 5,供关心的人使用:
App::missing(function($exception) {...});
在 Laravel 5 中不再可用,为了返回所有不存在的路由的 404 视图,请尝试将以下内容放入 app/Http/Kernel.php:
public function handle($request) {
try {
return parent::handle($request);
}
catch (Exception $e) {
echo \View::make('frontend_pages.page_404');
exit;
// throw $e;
}
}
【讨论】: