Laravel 4异常:NotFoundHttpException [英] Laravel 4 Exception: NotFoundHttpException

查看:55
本文介绍了Laravel 4异常:NotFoundHttpException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Laravel 4应用程序的日志有时显示NotFoundHttpException:

My Laravel 4 application's logs sometimes show a NotFoundHttpException:

exception 'Symfony\Component\HttpKernel\Exception\NotFoundHttpException' in /var/www/laravel4/bootstrap/compiled.php:7420
Stack trace:
#0 /var/www/laravel4/bootstrap/compiled.php(7137): Illuminate\Routing\Router->handleRoutingException(Object(Symfony\Component\Routing\Exception\ResourceNotFoundException))
#1 /var/www/laravel4/bootstrap/compiled.php(7113): Illuminate\Routing\Router->findRoute(Object(Illuminate\Http\Request))
#2 /var/www/laravel4/bootstrap/compiled.php(958): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#3 /var/www/laravel4/bootstrap/compiled.php(946): Illuminate\Foundation\Application->dispatch(Object(Illuminate\Http\Request))
#4 /var/www/laravel4/public/index.php(49): Illuminate\Foundation\Application->run()
#5 {main}

可能是什么原因造成的?堆栈跟踪未显示我的代码中的任何内容.

What could have caused this? The stack trace doesn't show anything from my code.

推荐答案

NotFoundHttpException基本上只是应用程序中的404.不幸的是,异常消息甚至都没有告诉您触发异常的请求的URL,这使得当您在日志中看到这些错误时,很难理解这些错误.

A NotFoundHttpException is basically just a 404 in your application. Unfortunately the exception message doesn't even tell you the URL of the request that triggered the exception, which makes it difficult to understand these errors when you see them in your logs.

要给您更多调试信息,请设置您自己的404处理程序.这是一个简单的处理程序,它将记录URL(以便您知道触发错误的请求)和用户代理(以帮助您确定谁或什么发出了请求)并返回视图和404代码:

To give you more debugging information, set up your own 404 handler. Here's a simple handler which will log the URL (so you know what was requested to trigger the error) and the user agent (to help you figure out who or what made the request) and return a view and 404 code:

App::missing(function($e) {
    $url = Request::fullUrl();
    $userAgent = Request::header('user-agent');
    Log::warning("404 for URL: $url requested by user agent: $userAgent");
    return Response::view('errors.not-found', array(), 404);
});

将其放入app/start/global.php.

这篇关于Laravel 4异常:NotFoundHttpException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆