Laravel 10 Custom 404, 500 Error Page Example

[ad_1]

Custom 404, 500 error page in Laravel 10. In this tutorial, we will show you how to create/build custom error pages in Laravel 10 app. And you will learn step by step how to create custom 404, 500 error page in laravel apps.

Usually, Laravel 10 display default error page 404, 500. But if you want to design of this pages. So, you can do it. This tutorial will guide you to step by step from scratch for how to create custom 404, 500 error pages in laravel

Sometimes, you need to create a blade files like 404.blade.php, 500.blade.php, etc on laravel apps. So navigate resources/views/errors) directory in laravel apps. And then create any custom error pages inside this folder.

Custom Error Pages in Laravel 10

  1. Create 404 View File
  2. Create 500 View File
  3. Modify Exceptions Handler

1: Create 404 View File

Go to resources/views folder and create a folder named errors.

Then inside this errors folder, create a file called 404.blade.php. So the location of our 404 page is resources/views/errors/404.blade.php.

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>

2: Create 500 View File

Go to resources/views folder and create a folder named errors.

Then inside this errors folder, create a file called 500.blade.php. So the location of our 500 page is resources/views/errors/500.blade.php.

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>

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:

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:

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

Laravel 10 create custom error page example, you have learned how to create custom page for 404, 500 error in laravel apps. As well as how to use them.

Recommended Laravel Posts

[ad_2]

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

GSTIN is 03EGRPS4248R1ZD.

Contact
Jassweb, Rai Chak, Punjab, India. 143518
Item added to cart.
0 items - 0.00