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

查看:267
本文介绍了如果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天全站免登陆