PNscripts.com

CALL NOW!(ID:274188)
+1-855-211-0932
HomeBlogLaravel custom 500 Error Page

Laravel custom 500 Error Page

Laravel makes it easy to display custom error pages for various HTTP status codes. For example, if you wish to customize the error page for 500 HTTP status code, create a resources/views/errors/500.blade.php view template. This view will be rendered on all 500 errors generated by your application.

Laravel Create Custom Error Page

Follow the below steps to create custom 500 error page in laravel:

  1. Create 500 error View File
  2. Modify Exceptions Handler

1. Create the View File

Go to resources/views folder and create a new folder (for example errors).

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

This is the custom 500 error page.

DOCTYPE html>
<html>
<head>
<title>Internal Server Error<title>
<head>
<body>
This is the custom 500 error page.
<body>
<html>

2. Modify Exceptions Handler

Now, navigate to app/Exceptions and open Handler.php file and find the render() method. Then modify the render() method as follow:

if you are useing Exception

public function render($request, Exception $exception)
{
if ($this->isHttpException($exception)) {
if ($exception->getStatusCode() == 500) {
return response()->view(‘errors/500’, [], 500);
}
}
return parent::render($request, $exception);
}

or if you are useing Throlable

public function render($request, Throwable $exception)
{
if ((!($exception instanceof HttpException) ||
($exception instanceof HttpException &&
$exception->getStatusCode() === 500)) &&
env(‘APP_DEBUG’) === false)) {
return response()->view(‘errors/500’, [], 500);
}
return parent::render($request, $exception);
}


Tags: ,

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>