Error pages

  • Out of the box Laravel supports all most common error pages:

Localization

Custom error pages

  • If you are satisfied with the (standard) layout of the error pages, you don't have to change anything
  • Maybe you want the pages in the same layout as the rest of the site
  • This can be done manually (see Laravel -> Custom error pages) or by publishing (= copying) all error pages from the framework to the website and then adjusting them to your own wishes
  • Publish all error pages to the website: php artisan vendor:publish --tag laravel-errors

Publish error pages

  • The first seven pages inside resources/views/errors/ are the error pages
  • The last three pages are Blade templates (only the last one, minimal.blade.php will be used at the moment)
  • First take a closer look at the code of 404.blade.php and 403.blade.php
// 404.blade.php
@extends('errors.minimal')
@section('title', __('Not Found'))
@section('code', '404')
@section('message', __('Not Found'))
1
2
3
4
5
// 403.blade.php
@extends('errors.minimal')
@section('title', __('Forbidden'))
@section('code', '403')
@section('message', __($exception->getMessage() ?: 'Forbidden'))
1
2
3
4
5
  • Laravel uses a special helper function __() to translate the error messages:
    • 404.blade.php: __('Not Found') refers to the string "Not Found": "Pagina niet gevonden", in resources/lang/nl.json
    • 403.blade.php: __($exception->getMessage() ?: 'Forbidden') first checks if the exception contains a custom error message. If there is no custom message, the string "Forbidden": "Verboden", in resources/lang/nl.json will be used
  • In order to give these error pages the same layout as the rest of the site, you have to point @extends to your own Blade template and put the other content in the appropriate section(s) of that template

Trigger an error (abort)

  • To test the exception, you're now going to simulate a 404 and a 403 error
    • Open routes/web.php and add a new route
// routes/web.php
Route::get('mySillyPage', function () {
    // abort with error 404
    return abort('404');
    // abort with error 403 (default error message)
    // return abort('403');
    // abort with error 403 (custom error message)
    // return abort('403', 'My Silly Error');
});
1
2
3
4
5
6
7
8
9
  • return abort('404'); aborts the rest of the code and sends you to the 404 page
  • return abort('403'); aborts the rest of the code and sends you to the 403 page, with the default error message
  • return abort('403', 'My Silly Error'); aborts the rest of the code and sends you to the 403 page, with a custom error message

Custom error messange

Last Updated: 11/13/2019, 8:29:21 PM