Laravel Error Pages: Matching HTTP Status Codes
In this article
Laravel's default error pages are functional but generic. When a visitor hits a 404, 403, or 500 error, they see a plain page with a small message. By creating custom error views and naming them after their HTTP status codes, you can display branded, helpful pages that match the actual error. This guide walks through the process, from locating the right directory to testing your new pages.
What You'll Need
- A Laravel application (version 8 or later recommended; the process is similar for older versions).
- Access to your project's
resources/viewsdirectory. - A basic understanding of Blade templates and HTTP status codes.
- Optional: a web browser to test the error pages.
Step-by-Step
-
Understand Laravel's error view resolution.
Laravel automatically looks for a Blade view in
resources/views/errors/that matches the HTTP status code. For example, a 404 error will look forresources/views/errors/404.blade.php. If that file doesn't exist, Laravel falls back to its default error page. -
Create the errors directory (if it doesn't exist).
Navigate to
resources/views/and check if anerrorsfolder exists. If not, create it. -
Create a view for each status code you want to customize.
For example, to create a custom 404 page, create a file named
404.blade.phpinside theerrorsfolder. You can create files for any status code:403.blade.php,500.blade.php,503.blade.php, etc. - Design your error page. Open the newly created file and write your HTML and Blade code. You can use your site's layout, include navigation, and add a friendly message. For example, a simple 404 page might look like: <!DOCTYPE html> <html> <head> <title>Page Not Found</title> </head> <body> <h1>404 - Page Not Found</h1> <p>Sorry, the page you're looking for doesn't exist.</p> </body> </html> For more advanced styling, you can use your main layout: @extends('layouts.app') and fill a content section.
- Access error details (optional). Laravel passes an $exception variable to error views. You can use it to display specific information, like the exception message (only for non-production environments) or the previous URL. For example, .
-
Handle maintenance mode (503).
For a 503 page, you might want to show a “Coming Soon” or “Under Maintenance” message. Laravel automatically uses
503.blade.phpwhen the app is in maintenance mode viaphp artisan down. - Test your error pages. To test locally, you can trigger errors. For a 404, visit a non-existent URL. For a 403, you can temporarily add a route that aborts with 403. For a 500, you can temporarily throw an exception in a route. Alternatively, you can use a tool like Postman or a browser extension to simulate requests.
-
Publish Laravel's default error pages (optional).
If you want to start from Laravel's built-in error page designs, you can publish them with the Artisan command:
php artisan vendor:publish --tag=laravel-errors. This copies the default error views into yourresources/views/errorsfolder, which you can then modify.
Common Mistakes
- Wrong file name: The file must be named exactly as the status code (e.g.,
404.blade.php, not404.bladeorerror404.blade.php). - Placing files in the wrong directory: The views must be in
resources/views/errors/, notresources/views/directly. - Forgetting to clear the view cache: If you've cached views (via
php artisan view:cache), your changes might not appear. Runphp artisan view:clearafter creating or editing error views. - Using
$exceptionin production: The exception message may contain sensitive information. Only display it ifAPP_DEBUGis true (which it shouldn't be in production). - Not testing all status codes: You might create a 404 page but forget 403 or 500. Test each one you intend to customize.
Tips
- Use a consistent design: Your error pages should match the rest of your site's branding. Use your main layout to include navigation and footer.
- Add helpful links: On a 404, include a search box or links to popular pages. On a 403, explain why access is denied and provide a contact link.
- Keep it simple: Don't overwhelm users with technical details. A clear message and a way to get back to the site is enough.
- Consider using a layout: Instead of writing a full HTML document in each error file, use
@extends('layouts.error')to keep your code DRY. - Log errors server-side: Even though you have custom pages, ensure you're logging the actual errors (via Laravel's log system) so you can debug issues.
Troubleshooting
- My custom error page isn't showing.
- Check the file name and location: it must be
resources/views/errors/{status}.blade.php. - Clear the view cache:
php artisan view:clear. - Ensure you're not using a route that catches all errors (e.g., a catch-all route that returns a response instead of aborting).
- Check the file name and location: it must be
- I get a blank page.
- Check the Blade syntax in your error view. A syntax error can cause a blank page.
- Check the Laravel logs (
storage/logs/laravel.log) for errors.
- My 503 page shows but the maintenance mode is off.
- If you have a
503.blade.phpfile, it will be used for any 503 error, not just maintenance mode. That's fine, but ensure you're not accidentally triggering a 503.
- If you have a
- Error page shows default Laravel page instead of mine.
- Double-check the status code. For example, a 404 might actually be a 403 if you have authorization middleware.
- If you're using a package that overrides error handling, it might take precedence.
Frequently Asked Questions
Can I use a single custom page for all errors?
Yes, you can create a fallback error page by naming it error.blade.php in the errors folder. Laravel will use it for any status code that doesn't have a specific view.
How do I pass additional data to the error view?
You can use the view()->share() method in a service provider, or you can use the render() method of an exception handler to pass data. Simpler: use $exception which is always available.
Will my custom error pages work in production?
Yes, as long as the view files are deployed with your application. Just ensure you don't display sensitive exception details when APP_DEBUG is false.
Can I use a different layout for error pages?
Yes, you can extend any layout you want. Just use @extends('layouts.custom') in your error view.
How do I test a 500 error locally?
Temporarily add a route that throws an exception, e.g., Route::get('/test-error', function() { throw new \Exception('Test'); });. Visit that URL and you'll see your 500 page.
What if I want to customize the error page for a specific exception type?
Laravel allows you to define a custom render method in the App\Exceptions\Handler class. You can check the exception type and return a custom response or view.