Laravel 5.2 : csrf token doesn't work
Make sure your route has the web milddleware applied to it.
Pretty much any route where you will want sessions, csrf protection, encrypted cookies, session errors, etc ... you will need the 'web' middleware group applied.
Check your routes.php file for the route group like so:
Route::group(['middleware' => 'web'], function () {
//
});
Update: Since 5.2.27 The RouteServiceProvider
now puts all your routes in routes.php
in a route group that has the web
middleware applied for you.
In Version 5.2 : You move Route into:
Route::group(['middleware' => ['web']], function () {
//Your route here
});
Have two way to use Token in form (https://laravel.com/docs/master/routing#csrf-protection):
// Vanilla PHP
<?php echo csrf_field(); ?>
// Blade Template Syntax
{{ csrf_field() }}
Or
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
use
{!! csrf_token() !!}
instead of
{{ csrf_token() }}