Laravel 5.2 : csrf token doesn't work

- 1 answer

Ad

Hi Why my csrf token value is null ? And when i don't use token i havent TokenMismatchException!!!! how can i fix it ?

Look at the image Please

I dug deeper and found that a session is not being registered in SessionServiceProvider. Is there something that needs to be enabled for this to work by default? Since I am a Laravel beginner, I am not sure how to follow the advice above. How do I make sure that my routes are added under the "web" group?

<form method="post" action="<?php echo url('/form'); ?>">
    <input type="hidden" name="_Token" value="{{ csrf_token() }}">
    <input type="text" name="Title" placeholder="Title"><br>
    <textarea rows="10" name="Content" placeholder="Content"></textarea><br>
    <input type="submit" value="Send">
</form>
Ad

Answer

Ad

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.

Ad
source: stackoverflow.com
Ad