13 Dec

laravel token mismatch exception

Redirect Laravel Token Mismatch Exception error page

laravel TokenMismatchException screen

Every Laravel 5.2 and Laravel 5.4 (UPDATE) developer has at some point seen the above default grey ‘TokenMismatchException error page’, which is usually caused when a protected page (which the user needs to be logged in to view) has not been refreshed for a long time and the users login session has timed-out. So the best option is to make the url redirect instead of showing this error message

NOTE: There is a slight difference between laravel 5.2 and 5.4 so I have included both versions of the code – $e and $exception.

So have you been wondering if there is anyway to customise the page or redirect to the default login/home page? Well it’s actually fairly easy to sort with a few changes to some of the default setting and files.

 Step 1:

The file which needs to be changed is in the following directory ‘app\Exceptions\Handler.php

Add this at the top around line 11, with this we are using the built in Laravel component.

Then find the render function and add the following.

Laravel 5.2

Laravel 5.4

So your final amended file should look like this.

Laravel 5.2 Version

Laravel 5.4 Version

 Step 2:

Also on your login page you may want to output the error message so add the following code to your blade view.

 Step 3:

Another option is to extend the default 120 minutes session timeout, so do this by editing the following file ‘config/session.php‘.

… and change it to

I hope this has helped anybody who has experienced the same issue when developing with Laravel.



Written by me@grafxflow

I am a freelance designer and developer of all things web - from CSS3 bootstrap responsive layouts through to WordPress and MVC CodeIgniter. I blog and tweet to hopefully share a little bit of knowledge that can help others around the web. Thanks for stopping by!

Follow grafxflow on twitter Follow grafxflow on github Follow grafxflow on google+
Posted in: <Laravel>

No Comments on "Redirect Laravel Token Mismatch Exception error page"

Leave a Reply

Your email address will not be published. Required fields are marked *

smart search