Laravel5.4中自定义404、503等错误页面

在Laravel中所有的异常都由Handler类处理,该类包含两个方法:reportrender,其中render方法将异常渲染到http响应中。

将app/Exceptions/Handler类中的render方法改为如下:

PHP

1

2

3

4

5

6

7

public function render($request, Exception $exception)

{

    if ($exception) {

        return response()->view('error.'.$exception->getStatusCode(), [],$exception->getStatusCode());

    }

    return parent::render($request, $exception);

}

然后在resources/view/error/下面新建错误页面,命名为{errorCode}.balde.php,其中{errorCode}为错误码。

然后访问一个不存在的路由,显示为你的404页面即为成功!

猜你喜欢

转载自blog.csdn.net/u013724078/article/details/81396559