Custom error pages 500.blade.php and 404.blade.php in Laravel
Content Index
Now that we know how to configure Vue and Laravel, let's move on to the next step.
In Laravel, we can easily customize error pages, such as 404 or 500 types, by creating the corresponding views in the folder:
resources/views/errors/
The view name must match the HTTP status code. For example:
resources/views/errors/404.blade.php
resources/views/errors/500.blade.php
404 Page Example
File: resources/views/errors/404.blade.php
@extends('layouts.app')
@section('content')
<div class="error-page">
<h1>500 Internal Server Error</h1>
<p>Please try again later.</p>
<a href="{{ url('/') }}">Go back to the home page</a>
</div>
@endsection500 Page Example
File: resources/views/errors/500.blade.php
@extends('layouts.app') @section('content') <div class="error-page"> <h1>500 Internal Server Error</h1> <p>Please try again later.</p> <a href="{{ url('/') }}">Go back to the home page</a> </div> @endsectionNote: The 500 error page is only displayed when Laravel's debug mode is disabled.
Summary
- We can customize 400x error pages (such as 401, 402, 403, 404, 405) and 500x.
- Customization is done by creating the resources/views/errors folder and the corresponding files with the status code.
- By simply creating the file, Laravel will display that view when the error occurs.
- It's important to add content to the view, otherwise it may appear blank. You can include styles, components, or any design you want.
- To see the 500 error page, you must ensure that Laravel is in production mode.
- To customize other error codes, simply create files like 401.blade.php, 403.blade.php, etc.
- With this, you will have total control over the appearance of your error pages, avoiding showing Laravel's default format and improving the user experience.
The next step is to learn how to use polymorphic relationships in Laravel.
I agree to receive announcements of interest about this Blog.
We will see how we can create custom pages to handle 50X and 40X errors in a personalized way.