laravel实现前后端跳转不同的404页面

自己的博客,想前端和后端跳转不同的页面,laravel异常处理都在app/Excetions里,打开Handler.php,对于所有的HTTP异常都是用 renderHttpException() 来处理的,Handler.php里没有,追溯到父类 找到 renderHttpException(); 如果存在 errors.{httpStatusCode} 的视图,它就会直接显示该视图 如果一般的想要跳转404页面,直接在resources/errors下建立自己的404页面即可! 此时,可以修改为

 protected function renderHttpException(HttpException $e)
    {
        $status = $e->getStatusCode();

        if (Auth::check()) { //如果登陆跳转后台的404
            if (view()->exists("errors.{$status}")) {
                return response()->view("errors.{$status}Admin", ['exception' => $e], $status);
            } else {
                return $this->convertExceptionToResponse($e);
            }
        } else { //如果没有登陆,跳转前端的404
            if (view()->exists("errors.{$status}")) {
                return response()->view("errors.{$status}Home", ['exception' => $e], $status);
            } else {
                return $this->convertExceptionToResponse($e);
            }
        }

    }

Auth:check() 检验当前用户是否登陆,登陆的话,跳转errors.404Admin页面。没登陆,就跳转errors.404Home页面,这个页面自定义, 当然了,resources/errors下面必须有三个文件,一个404.blade.php 两个404XXX.blade.php 页面


欢迎转载,但请附上原文地址哦,尊重原创,谢谢大家 本文地址: http://www.iphpt.com/detail/2/

当你能力不能满足你的野心的时候,你就该沉下心来学习