Laravel 4-仅针对缺少页面的自定义404错误处理 [英] Laravel 4 - custom 404 error handling only for missing pages

查看:71
本文介绍了Laravel 4-仅针对缺少页面的自定义404错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

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

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:

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

因此,问题是:仅当页面丢失时,如何才能丢弃404,而其余部分(图像,其他文件等)如何保留默认错误处理?

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.)?

提前谢谢!

推荐答案

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

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:

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

这会在页面加载时显示正确的视图,但是由于HTTP 404 Status Code,会迫使浏览器在尝试加载资源时抛出错误.

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.

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

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

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

这篇关于Laravel 4-仅针对缺少页面的自定义404错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆