# Custom error pages

# Publish the default Laravel error pages

  • Use the php artisan vendor:publish --tag=laravel-errors command to publish the default Laravel error pages
  • All error pages are now available in the resources/views/errors folder
  • Open, for example, the resources/views/errors/404.blade.php file
  • This and all other error pages @extends the errors::minimal template, located in the same folder

REMARKS

Laravel actually has two ways of creating layout templates (opens new window)

  • Component based layouts:
    • This is the "modern" way of creating layouts (like we did in the previous sections)
    • It uses (named) slots like <x-slot name='nameOfTheGap'> to inject content into the layout
  • Layouts using template inheritance:
    • This is the "old" way of creating layouts (like the template used for the error pages)
    • It uses @yield('nameOfTheGap') to inject content into the layout
  • If we modify the resources/views/errors/minimal.blade.php file, all error pages will be updated because they are using this template
  • Open the resources/views/errors/minimal.blade.php file and replace the content with the following code:
    • The layout is now based on the <x-vinylshop-layout> template
    • But we keep the @yield('code') and the @yield('message')from the original template to inject the error code and message




 


 












<x-vinylshop-layout>
  <x-slot name="title"></x-slot>
  <div class="grid grid-rows-2 grid-flow-col gap-4">
      <p class="row-span-2 text-5xl text-right border-r-2 border-gray-700 pr-4">
          @yield('code')
      </p>
      <p class="text-2xl font-light text-gray-400">
          @yield('message')
      </p>
      <div>
          <x-jet-button class="bg-gray-400 hover:bg-gray-500">
              <a href="{{ route('home') }}">Home</a>
          </x-jet-button>
          <x-jet-button class="bg-gray-400 hover:bg-gray-500">
              <a href="#" onclick="window.history.back();">Back</a>
          </x-jet-button>
      </div>
  </div>
</x-vinylshop-layout>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

Rerun vite

  • After publishing the default Laravel error pages it is necessary to restart the compilation (Vite), otherwise the Tailwind classes aren't interpreted and you won't get the expected lay-out

# Session on 404 error page

  • Even if you are logged in, the user navigation on the right is missing on the 404 page (while on the 403 and 500 error pages there's no problem)
  • This is because Laravel doesn't load the session on a regular 404 error and therefore doesn't know anything of the auth state (more details about sessions later in this course)
  • In order to not confuse the user, it is better to remove the right navigation completely
  • Open the resources/views/errors/404.blade.php file
  • Push a script to script stack:






 
 
 
 
 
 

@extends('errors::minimal')

@section('title', __('Not Found'))
@section('code', '404')
@section('message', __('Not Found'))

@push('script')
    <script>
        // remove the last div inside the nav tag
        document.querySelector('header nav  div:last-child').remove();
    </script>
@endpush
1
2
3
4
5
6
7
8
9
10
11
12

Hide right navigation on 404

Last Updated: 11/16/2022, 12:49:35 PM