January 26, 2021
When I moved my personal website to Laravel, I wanted to make sure that I hadnāt forgotten to move some important routes. Therefore, I wanted to keep a log of all the ā404 not foundā errors that happened on my website.
I was originally going to listen for the error to be thrown within the app, but then found out that Laravel ignores 404 errors for you.
Instead, I settled on a hacky (but simple and effective!) solution:
You can customize Laravelās 404 error page by creating a view at resources/views/errors/404.blade.php
. I built a simple ānot foundā page there, and then wrote the PHP code I wanted to run in the view within a @php
blade directive in that view:
{{-- resources/views/errors/404.blade.php --}}
@php
$array = Cache::get('404') ?? []; $array[] = '/' . request()->path(); Cache::forever('404', $array);
@endphp
This code adds the current URL to an array thatās stored in the cache, so I can see which URLs people are going to but that donāt exist.