Laravel 4 异常:NotFoundHttpException

2022-01-08 00:00:00 php symfony laravel laravel-4

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

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

exception 'SymfonyComponentHttpKernelExceptionNotFoundHttpException' in /var/www/laravel4/bootstrap/compiled.php:7420
Stack trace:
#0 /var/www/laravel4/bootstrap/compiled.php(7137): IlluminateRoutingRouter->handleRoutingException(Object(SymfonyComponentRoutingExceptionResourceNotFoundException))
#1 /var/www/laravel4/bootstrap/compiled.php(7113): IlluminateRoutingRouter->findRoute(Object(IlluminateHttpRequest))
#2 /var/www/laravel4/bootstrap/compiled.php(958): IlluminateRoutingRouter->dispatch(Object(IlluminateHttpRequest))
#3 /var/www/laravel4/bootstrap/compiled.php(946): IlluminateFoundationApplication->dispatch(Object(IlluminateHttpRequest))
#4 /var/www/laravel4/public/index.php(49): IlluminateFoundationApplication->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中.

相关文章