laravel 5.5中后端站点的自定义404错误页面

时间:2018-08-13 06:23:35

标签: laravel-5 routes

我想为前缀后端创建自定义404页面。但它只显示前端站点404错误页面..当后端/ 404-URL我想要后端时。布局404 ..

Route::prefix('/backend')->group(function(){

    Route::get('/test','BackController@index');
    Route::get('/home','BackController@home');
    Route::resources([
    'categories' => 'CategoryController',
    'posts' => 'PostController',
    'users'=>'UserController'
]);
});

1 个答案:

答案 0 :(得分:0)

如果异常是ModelNotFoundException或NotFoundHttpException之一,则应在app / Exceptions / Handler.php中添加/修改render方法,以返回ajax请求的json响应或正常请求的视图。

因此您的代码将如下所示:

protected function renderHttpException(HttpException $e) {

    $status = $e->getStatusCode();

    if(Request::is('/backend/*')) { //Chane to your backend your !
        return response()->view("backend/errors.{$status}", ['exception' => $e], $status, $e->getHeaders());
    }else {
        return response()->view("errors.{$status}", ['exception' => $e], $status, $e->getHeaders());
    }

}

一些链接可以帮助您实现这一目标:

Laravel Documentation on errors

Scoth.io tutorial for creating a 404 page using custom exception handler