To create custom 404, 500 error pages in Laravel; Simply navigate to the resources/views/ directory and create a 404,500 error page and then navigate to the app/exceptions/handler.php file and add your new custom 404.blade.php and 500.blade.php.
Whenever some technical error occurs in the Laravel web application. Or a user hits any URL in the browser. So that URL or page is not found. In this case, Laravel Web Applications displays 404 and 500 error pages to the user.
How to Create Custom 500, 404 Page in Laravel 11 / 10
Steps to create and use custom 404 and 500 error page in laravel 11 / 10 applications:
Step 1: Create 404 Error Page
Firstly, open your laravel web applications in any text editor. Then visit to resources/views directory and create a folder named errors.
Once you have created errors directory in your laravel web applications. Then visit inside this errors directory, create a file called 404.blade.php. And add the following code into it:
resources/views/errors/404.blade.php
<!DOCTYPE html> <html> <head> <title>Page Not Found</title> </head> <body> This is the custom 404 error page. </body> </html>
Step 2: Create 500 Error Page
Firstly, open your laravel web applications in any text editor. Then visit to resources/views directory and create a folder named errors.
Once you have created errors directory in your laravel web applications. Then visit inside this errors directory, create a file called 500.blade.php. And add the following code into it:
resources/views/errors/500.blade.php
<!DOCTYPE html> <html> <head> <title>Page Not Found</title> </head> <body> This is the custom 500 error page. </body> </html>
Step 3: Use 404 and 500 Error Page In Laravel 11 / 10
Now, visit to app/Exceptions
directory and open Handler.php
file. And find the render() method in it. Then modify the render() method only as follow:
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, you can render 500 error page in this file as follow:
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); }
Conclusion
That’s it. you have learned how to create custom page for 404, 500 error in laravel apps. As well as how to use them.