Laravel 4 Exception: NotFoundHttpException

2019-02-04 21:00发布

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.

6条回答
We Are One
2楼-- · 2019-02-04 21:32

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

查看更多
Ridiculous、
3楼-- · 2019-02-04 21:35

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.

查看更多
ゆ 、 Hurt°
4楼-- · 2019-02-04 21:37

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.

查看更多
We Are One
5楼-- · 2019-02-04 21:41

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.

查看更多
别忘想泡老子
6楼-- · 2019-02-04 21:49

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

Might be useful to someone.

查看更多
我只想做你的唯一
7楼-- · 2019-02-04 21:54

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
});
查看更多
登录 后发表回答