During this lesson, you'll learn how to customize error handling using Laravel’s exception handler. This involves setting up error views, like a custom 404 page, to provide users with clear information and a smooth experience even when errors occur. To remind you, error handling is not a new concept; it's similar to setting up middleware in that both enhance app reliability.
Ever visited a website and encountered a 404 error when you tried to access a page that doesn't exist? Now, let's imagine you're the developer of that website and you want a custom 404 page to guide users back to the main application. This is where error handling comes in. By customizing error messages, you can provide users with a clear path back to the main application, ensuring they don't get lost or frustrated.
Let's see an example of custom error page in Laravel:
<!DOCTYPE html>
<html>
<head>
<title>Page Not Found</title>
</head>
<body>
<h1>404 - Page Not Found</h1>
<p>Sorry, the page you are looking for could not be found.</p>
<a href="{{ url('/todos') }}">Back to ToDo List</a>
</body>
</html>
Here, we've created a custom 404 page that displays a message and a link to the main application.
Now, let's see how we can integrate this custom error page into our Laravel application. To do that, we need to modify the app/Exceptions/Handler.php file which contains the exception handler for our application.
<?php
namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Throwable;
class Handler extends ExceptionHandler
{
protected $dontReport = [];
protected $dontFlash = [
'current_password',
'password',
'password_confirmation',
];
public function register()
{
$this->renderable(function (ModelNotFoundException $e, $request) {
return response()->view('errors.404', [], 404);
});
}
}
Let's break down the code snippet above:
- We've created a custom 404 page in the
resources/views/errors directory named 404.blade.php.
- In the handler we have a class
Handler that extends ExceptionHandler.
- The
dontReport property contains an array of exceptions that should not be reported. We've left it empty for now.
- The
dontFlash property contains an array of inputs that should not be flashed to the session - in this case the password fields.
- The
register method is used to register custom exception handling logic. Here, we're using the renderable method to handle the ModelNotFoundException exception. When this exception is thrown, the custom 404 view is returned with a 404 status code.
- The
response()->view() method is used to return a view response with the custom 404 view and a 404 status code.
Note, that by default Laravel will show the content of the resources/views/errors/404.blade.php file when a 404 error occurs, even if you don't have a custom exception handler defined in the Handler class.
There is a nuance in Laravel error handling that you should be aware of. If the custom error file name is different from 404.blade.php, you need to update the render method in the Handler class to handle the custom error file. For example, if you have a custom error file named custom-404.blade.php, you would update the render method instead of the register method:
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
...
public function render($request, Throwable $exception)
{
if ($exception instanceof NotFoundHttpException) {
return response()->view('errors.custom-404', [], 404);
}
return parent::render($request, $exception);
}
In this code snippet, we're checking if the exception is an instance of NotFoundHttpException. If it is, we return the custom 404 view with a 404 status code. Otherwise, we call the parent render method to handle the exception.
With this, when a 404 error occurs, the custom 404 view will be displayed to the user with whatever message you've defined in the view file.