Laravel 5.2 如何将所有 404 错误重定向到主页

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

Laravel 5.2 How To redirect All 404 Errors to Homepage

laravelredirecthttp-status-code-404http-status-code-301laravel-5.2

提问by Buglinjo

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

如何将所有 404 错误重定向到主页?我有自定义错误页面,但谷歌分析抛出了太多错误。

回答by shahalpk

For that, you need to do add few lines of code to rendermethod in app/Exceptions/Handler.phpfile.

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

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);
    }
}

回答by LuizEduardoMPF

For me with php 7.2 + Laravel 5.8, it works like a boss. I changed the render method (app/Exceptions/Handler.php). Therefore, we have to check if the exception is an HTTP exception, because we are calling the getStatusCode () method, which is available only in HTTP exceptions. If the status code is 404, we may return a view (for example: errors.404) or redirect to somewhere or route (home).

对于使用 php 7.2 + Laravel 5.8 的我来说,它就像一个老板。我更改了渲染方法 (app/Exceptions/Handler.php)。因此,我们必须检查异常是否为HTTP异常,因为我们调用的是getStatusCode()方法,该方法仅在HTTP异常中可用。如果状态码是 404,我们可能会返回一个视图(例如:errors.404)或重定向到某个地方或路由(家)。

app/Exceptions/Handler.php

应用程序/异常/Handler.php

public function render($request, Exception $exception)
    {

        if($this->isHttpException($exception)) {
            switch ($exception->getStatusCode()) {
                // not found
                case 404:
                    return redirect()->route('home');
                    break;

                // internal error
                case 500:
                    return \Response::view('errors.500', [], 500);
                    break;

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

    }

To test: Add abort(500); somewhere in the flow of your controller to view the page/route. I used 500, but you can use one of errors code: Abort(404)...

测试:添加 abort(500); 在控制器流程中的某处查看页面/路由。我使用了 500,但您可以使用错误代码之一:Abort(404)...

abort(500);

Optionally, we may provide a response:

或者,我们可以提供回复:

abort(500, 'What you want to message');