php Laravel 4 异常:NotFoundHttpException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15843265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:03:53  来源:igfitidea点击:

Laravel 4 Exception: NotFoundHttpException

phpsymfonylaravellaravel-4

提问by Nyxynyx

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

我的 Laravel 4 应用程序的日志有时会显示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.

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

回答by Andreas

A NotFoundHttpExceptionis 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.

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

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:

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

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.

把它放进去app/start/global.php

回答by Mark

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.

我遇到了这个问题,因为我将父目录的名称大写,而没有在 URL 中大写。这就是我收到错误的原因。

I was using this url localhost/laravel/todo2/public/fooand 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.

我正在使用这个 url localhost/laravel/todo2/public/foo并且实际上如果我输入的所有内容都是: localhost/Laravel/todo2/public/,但如果我在 public/ 之后输入任何内容,它会给出该错误。

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

如果我将它与 Laravel 大写的 localhost/Laravel/todo2/public/foo 一起使用

the routes after public/ would work

public/ 之后的路线会起作用

回答by Combine

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

我的项目在C:\xampp\htdocs\ColorTex

I got this error by typing

我通过输入得到这个错误

http://localhost/myproject/public/image

http://localhost/myproject/public/image

instead of

代替

http://localhost/MyProject/public/image

http://localhost/MyProject/public/image

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

我在 Windows 中,所以我认为它会忽略大小写,但是......

Be careful with that.

小心点。

回答by vFragosop

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.

在不知道您的代码的情况下,很难确定是什么导致了这种情况。查看错误的堆栈跟踪,很明显路由器正在调度它,我猜这与后台作业本身无关,而是它调用的 API。

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

在您的特定情况下,异常是在ResourceNotFoundException之后引发的,当没有为此类 URL 模式定义路由时会发生这种情况。

Possible problems:

可能的问题:

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

如果后台作业在您自己的 API 上执行file_get_contentscurl执行,则它可能正在调用不存在的路由,然后抛出该异常。

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, localhostor anything like that.

如果你不具备URL的后台作业正在下载的控制,它可能试图获取像一个URL 127.0.0.1localhost或者类似的东西。

回答by Shojib Flamon

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

您可以从日志文件中过滤 404 (NotFoundHttpException) 错误。
文件位置: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
});

回答by xyLuz

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

我收到此错误是因为我在表单中使用了 method="post" 而不是 method="POST"。

Might be useful to someone.

可能对某人有用。