How to Create Friendly Laravel Error Pages with Custom Debug Handling
This guide explains how to replace Laravel's default unfriendly error screens with a custom, user‑friendly page that adapts to the debug setting, displays helpful messages, and automatically redirects after a short delay.
By default Laravel shows unfriendly error pages whether debug mode is on or off.
When debug is enabled the error page looks like the following:
When debug is disabled the error page looks like this:
You can customize the response by editing app/Exceptions/Handler.php and overriding the render method.
public function render($request, Exception $e)
{
$debug = config('app.debug', false);
if (empty($debug)) {
// When debug is false, return a friendly page
return response()->view('errors.503', ['info' => '好像走错了','url'=>'/']);
}
if ($e instanceof ModelNotFoundException) {
$e = new NotFoundHttpException($e->getMessage(), $e);
}
return parent::render($request, $e);
}The view errors/503.blade.php can contain a script that redirects after three seconds:
<script>
setTimeout(function(){
window.location.href="{{$url or '/'}}";
},3000)
</script>This ensures that any error will be displayed on a custom friendly page, showing a message and automatically redirecting after a short delay, which can be further styled for better user experience.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
