Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4 Exception: 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.

like image 298
Nyxynyx Avatar asked Apr 05 '13 20:04

Nyxynyx


People also ask

What is Exception handling in Laravel?

By default, the Laravel exception handler will convert exceptions into an HTTP response for you. However, you are free to register a custom rendering closure for exceptions of a given type. You may accomplish this via the renderable method of your exception handler.

What is NotFoundHttpException?

A NotFoundHttpException is basically just a 404 in your application.

What is route filter in Laravel?

Route filters provide a convenient way of limiting access to a given route, which is useful for creating areas of your site which require authentication. There are several filters included in the Laravel framework, including an auth filter, an auth. basic filter, a guest filter, and a csrf filter.


1 Answers

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.

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

Put it in app/start/global.php.

like image 51
Andreas Avatar answered Sep 29 '22 04:09

Andreas