Laravel 4 - 仅针对缺失页面的自定义 404 错误处理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24185735/
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-09-14 09:38:13  来源:igfitidea点击:

Laravel 4 - custom 404 error handling only for missing pages

phplaravellaravel-4

提问by Sas Sam

In Laravel 4.1 I want to redirect the user to my customised 404 page if the page is not exists. That's easy because I already have this:

在 Laravel 4.1 中,如果页面不存在,我想将用户重定向到我自定义的 404 页面。这很容易,因为我已经有了这个:

App::missing(function($exception)
{
    return Redirect::to('error404', 303)->with(array('error' => array('url' => Request::url())));
}

But the problem is that if any linked file (image, js, css, etc.) is missing on the page I don't get 404 error for that file, but this is showing on console:

但问题是,如果页面上缺少任何链接文件(图像、js、css 等),我不会收到该文件的 404 错误,但这会显示在控制台上:

Resource interpreted as Image but transferred with MIME type text/html: "...

Resource interpreted as Image but transferred with MIME type text/html: "...

So the question is: How can I drop 404 only if a page is missing and leave the default error handling for the rest (images, other files, etc.)?

所以问题是:如何仅在页面丢失时删除 404 并为其余页面(图像、其他文件等)保留默认错误处理?

Thanks in advance!

提前致谢!

回答by rmobis

You could not redirect to a different URL, but still display a custom page for 404 errors and send the HTTP 404 Status Code, directly inside App::missing, like this:

您无法重定向到不同的 URL,但仍会显示 404 错误的自定义页面,并HTTP 404 Status Code直接将,发送到内部App::missing,如下所示:

App::missing(function() {
    return Response::make(View::make('error404'), 404);
});

This displays the correct view when a page is loaded but forces the browser to throw an error when trying to load a resource, because of the HTTP 404 Status Code.

这会在页面加载时显示正确的视图,但会强制浏览器在尝试加载资源时抛出错误,因为HTTP 404 Status Code.

On Chrome 35, I get this error on the console when trying to load an image in an imgtag:

在 Chrome 35 上,尝试在img标签中加载图像时,我在控制台上收到此错误:

GET http://testing.app:8000/test 404 (Not Found)

回答by Goldentoa11

I would check to see what the Requestwants in the App::missingmethod. Not sure if this would work right out of the box, but it's a start.

我会检查以查看RequestApp::missing方法中的需求。不确定这是否开箱即用,但这是一个开始。

App::missing(function($exception)
{
    if( Request::format() == 'text/html' )
        return Redirect::to('error404', 303)->with(array('error' => array('url' => Request::url())));
}

Edit

编辑

Since the code above doesn't do what you want, I revised it to something else. It feels like a hack and I would not recommend using it in production; I would recommend looking for a better way to do this, but I think this will work. (It does for me)

由于上面的代码没有做你想要的,我把它改成了别的东西。这感觉就像一个黑客,我不建议在生产中使用它;我建议寻找更好的方法来做到这一点,但我认为这会奏效。(它对我有用)

App::missing(function($exception)
{
    $uri = Request::getRequestUri();
    $ext = explode(".", $uri);

    if( preg_match("/(jpg|gif|png|tif)/", end($ext)) !== 1 )
        return Redirect::to('error404', 303)->with(array('error' => array('url' => Request::url())));
});

Part of the reason this is difficult to do is because the application doesn't know what a request string really shouldreturn. For example, a request uri of /images/FJB3cB09could possibly be an image. A uri of /images/my-profile.jpgdoesn't necessarily have to return a JPEG image. It can't know what the content type is until after it reads the contents of the file; if there's no file found, then you can't accurately tell what you were trying to access. Make sense?

这很难做到的部分原因是因为应用程序不知道请求字符串真正应该返回什么。例如,请求 uri/images/FJB3cB09可能是一个图像。的 uri/images/my-profile.jpg不一定必须返回 JPEG 图像。它在读取文件内容后才能知道内容类型是什么;如果没有找到文件,那么您就无法准确判断您要访问的内容。有道理?