13

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.

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
Nyxynyx
  • 61,411
  • 155
  • 482
  • 830
  • Two related possibilities - (1) if your site is available on the web (or on your network) someone else may be hitting your site with an invalid URL which is generating the NotFound error or (2) something on your machine may be hitting localhost with an invalid url. Either of which would mean it's not your code and not your problem. – J.T. Grimes Apr 05 '13 at 22:14

6 Answers6

26

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.

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
Andreas
  • 7,991
  • 2
  • 28
  • 37
3

I got this problem because I had capitalized the name of a parent directory and didn't capitalize it in the URL. That's why I got the error.

I was using this url localhost/laravel/todo2/public/foo and in fact it worked if all I typed was: localhost/Laravel/todo2/public/ but if I typed anything after the public/ it would give that error.

if I used this with Laravel capitalized localhost/Laravel/todo2/public/foo

the routes after public/ would work

Mark
  • 31
  • 1
1

It's really hard to say for sure what is causing this without knowing your code. Looking at the stack trace of the error, it's clear that the Router is dispatching it and I guess it has nothing to do with the background job itself, but rather the API's it calls.

In your specific case, the exception is throw after a ResourceNotFoundException, which happens when there's no route defined for such URL pattern.

Possible problems:

If the background job does a file_get_contents or curl on your own API's, it may be calling a route that does not exist and then throwing that exception.

If you don't have control of the URL's the background job is downloading, it may be trying to fetch an URL like 127.0.0.1, localhost or anything like that.

vFragosop
  • 5,705
  • 1
  • 29
  • 31
  • You may also be requesting the URL with a different method than specified in your routes, eg. if you've made a route with a `POST` and you're trying to `GET` it, that will fail. – Joel Mellon Jan 22 '14 at 23:28
1

My project is in C:\xampp\htdocs\ColorTex

I got this error by typing

http://localhost/myproject/public/image

instead of

http://localhost/MyProject/public/image

I'm in windows, so I thought it will ignore capitalization, but...

Be careful with that.

Combine
  • 3,894
  • 2
  • 27
  • 30
1

You can filter 404 (NotFoundHttpException) error form your log file.
File location : app/start/global.php

App::error(function(Exception $exception, $errorCode)
{
    $requestUrl = Request::fullUrl(); 
    $userAgent = Request::header('user-agent');

    if($errorCode != 404){
        Log::error('Exception', array(
            'errorCode' => $errorCode,
            'requestUrl' => $requestUrl,
            'userAgent' => $userAgent,
            'context' => $exception,
        ));     
    }

    return Response::view('error-page-path.error-404', array(), 404);
    // Here "error-404" is a blade view file in "error-page-path" directory
});
Shojib Flamon
  • 1,237
  • 3
  • 12
  • 18
0

I got this error because i used method="post" in my form instead of method="POST".

Might be useful to someone.

xyLuz
  • 178
  • 1
  • 11