如果 Laravel 5 中不存在路由,则重定向到主页 [英] Redirect to homepage if route doesnt exist in Laravel 5

查看:24
本文介绍了如果 Laravel 5 中不存在路由,则重定向到主页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/** Redirect 404's to home
*****************************************/
App::missing(function($exception)
{
    // return Response::view('errors.missing', array(), 404);
    return Redirect::to('/');
}); 

我的 routes.php 文件中有此代码.我想知道如果出现 404 错误如何重定向回主页.这可能吗?

I have this code in my routes.php file. I am wondering how to redirect back to the home page if there is a 404 error. Is this possible?

推荐答案

为此,您需要在 app/Exceptions/Handler.php 文件中添加几行代码来渲染方法,如下所示:

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

public function render($request, Exception $e)
    {
        if($this->isHttpException($e))
        {
            switch ($e->getStatusCode()) 
                {
                // not found
                case 404:
                return redirect()->guest('home');
                break;

                // internal error
                case '500':
                return redirect()->guest('home');
                break;

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

这篇关于如果 Laravel 5 中不存在路由,则重定向到主页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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