Laravel 默认路由到 404 页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26770156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel default route to 404 page
提问by dulan
I'm using Laravel 4 framework and I've defined a whole bunch of routes, now I wonder for all the undefined urls, how to route them to 404 page?
我正在使用 Laravel 4 框架并且我已经定义了一大堆路由,现在我想知道所有未定义的 url,如何将它们路由到 404 页面?
采纳答案by bgallagh3r
Undefined routes fires the Symfony\Component\HttpKernel\Exception\NotFoundHttpException
exception which you can handle in the app/start/global.php using the App::error() method like this:
未定义的路由会触发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');
});
回答by Chutipong Roobklom
In Laravel 5.2. Do nothing just create a file name 404.blade.php in the errors folder , it will detect 404 exception automatically.
在 Laravel 5.2 中。什么都不做,只需在错误文件夹中创建一个文件名 404.blade.php ,它会自动检测 404 异常。
回答by akrist
The recommended method for handling errors can be found in the Laravel docs:
处理错误的推荐方法可以在 Laravel 文档中找到:
http://laravel.com/docs/4.2/errors#handling-404-errors
http://laravel.com/docs/4.2/errors#handling-404-errors
Use the App::missing() function in the start/global.php file in the following manner:
按照以下方式使用 start/global.php 文件中的 App::missing() 函数:
App::missing(function($exception)
{
return Response::view('errors.missing', array(), 404);
});
回答by verax
according to the official documentation
根据官方文档
you can just add a file in: resources/views/errors/ called 404.blade.php with the information you want to display on a 404 error.
您可以在:resources/views/errors/ 中添加一个名为 404.blade.php 的文件,其中包含您希望在 404 错误时显示的信息。
回答by dulan
I've upgraded my laravel 4 codebase to Laravel 5, for anyone who cares:
对于任何关心的人,我已将 Laravel 4 代码库升级到 Laravel 5:
App::missing(function($exception) {...});
is NO LONGER AVAILABLE in Laravel 5, in order to return the 404 view for all non-existent routes, try put the following in app/Http/Kernel.php:
在 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;
}
}