我們都知道,當 APP_DEBUG = false 時,我們展示的錯誤頁面是這樣的:

那我們怎么來自定義這個頁面呢?其實很簡單,打開 app/Exceptions/Handler.php 我們來重寫下這個文件
~~~
<?php
namespace App\Exceptions;
use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\Debug\ExceptionHandler as SymfonyDisplayer;
class Handler extends ExceptionHandler
{
/**
*
* @param \Exception $e
*
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function convertExceptionToResponse(Exception $e)
{
$debug = config('app.debug', false);
if ($debug) {
// 當 debug 為 true 時,返回默認的報錯頁面
return (new SymfonyDisplayer($debug))->createResponse($e);
}
return response()->view('errors.default', ['expection' => $e], 500);
}
}
~~~
然后就可以在 views/errors 下新建你的 default.blade.php 文件,自定義你頁面吧