In this tutorial you will learn about the Laravel 7 Custom 404, 500 Error Page Example and its application with practical example.
In this Laravel 7 Custom 404, 500 Error Page Example tutorial I’ll show you how to create custom 404 and 500 error page in laravel. In this tutorial you will learn to create custom error page in laravel. In this step by step tutorial I’ll demonstrate the process to create custom 404 and 500 error page in laravel.
Laravel Create Custom Error Page
In this tutorial we will be creating custom error page for 404 and 500 erro. Please Follow the instruction given below to create custom 404, 500 error pages in laravel aps:
Create 404 View File
Create 500 View File
Modify Exceptions Handler
1: Create 404 View File
In this step we will be creating custom 404 error page. Lets go to resources/views directory and create a folder named errors. Then inside the errors folder, we will create a file called 404.blade.php.
resources/views/errors/404.blade.php
1 2 3 4 5 6 7 8 9 |
<!DOCTYPE html> <html> <head> <title>Page Not Found</title> </head> <body> This is the custom 404 error page. </body> </html> |
2: Create 500 View File
In this step we will be creating custom 500 error page. Lets Go to resources/views directory and create a folder named errors. Then inside the errors folder, we will create a file called 500.blade.php.
resources/views/errors/500.blade.php
1 2 3 4 5 6 7 8 9 |
<!DOCTYPE html> <html> <head> <title>Page Not Found</title> </head> <body> This is the custom 500 error page. </body> </html> |
3: Modify Exceptions Handler
Now, navigate to app/Exceptions and open Handler.php file and find the render() method. Then modify the render() method only as follow:
1 2 3 4 5 6 7 8 9 |
public function render($request, Exception $exception) { if ($this->isHttpException($exception)) { if ($exception->getStatusCode() == 404) { return response()->view('errors.' . '404', [], 404); } } return parent::render($request, $exception); } |
as well as render 500 error page in this file as follow:
1 2 3 4 5 6 7 8 9 10 11 12 |
public function render($request, Exception $exception) { if ($this->isHttpException($exception)) { if ($exception->getStatusCode() == 404) { return response()->view('errors.' . '404', [], 404); } if ($exception->getStatusCode() == 500) { return response()->view('errors.' . '500', [], 500); } } return parent::render($request, $exception); } |