Laravel 5.2如何将所有404错误重定向到首页 [英] Laravel 5.2 How To redirect All 404 Errors to Homepage

查看:445
本文介绍了Laravel 5.2如何将所有404错误重定向到首页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将所有404错误重定向到首页?我有自定义错误页面,但Google Analytics(分析)抛出了太多错误.

How can I redirect all 404 errors to homepage? I have custom Error Page but google analytics is throwing too much errors.

推荐答案

为此,您需要在app/Exceptions/Handler.php文件中的render方法中添加几行代码.

For that, you need to do add few lines of code to render method in app/Exceptions/Handler.php file.

public function render($request, Exception $e)
{   
    if($this->isHttpException($e))
    {
        switch (intval($e->getStatusCode())) {
            // not found
            case 404:
                return redirect()->route('home');
                break;
            // internal error
            case 500:
                return \Response::view('custom.500',array(),500);
                break;

            default:
                return $this->renderHttpException($e);
                break;
        }
    }
    else
    {
        return parent::render($request, $e);
    }
}

这篇关于Laravel 5.2如何将所有404错误重定向到首页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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