Framework: Empty CSRF-Token on Laravel 5.2

Created on 28 Dec 2015  路  10Comments  路  Source: laravel/framework

Both the csrf_token() and csrf_field() functions/helpers are returning an empty value on a fresh Laravel 5.2 installation.

Edit: thanks Dan! That did the job :)

Most helpful comment

On a fresh install, make sure your route is within web middleware group

<?php

Route::get('foo', function () {
    return csrf_token(); // null
});

Route::group(['middleware' => 'web'], function () {
    Route::get('bar', function () {
        return csrf_token(); // works
    });
});

Otherwise the StartSession middleware isn't loaded and so no token exists on the session.

All 10 comments

Are you sure the route you're accessing uses StartSession middleware?

On a fresh install, make sure your route is within web middleware group

<?php

Route::get('foo', function () {
    return csrf_token(); // null
});

Route::group(['middleware' => 'web'], function () {
    Route::get('bar', function () {
        return csrf_token(); // works
    });
});

Otherwise the StartSession middleware isn't loaded and so no token exists on the session.

I am also faced same Issue? Above solutions are not worked.

Are you sure you're using Laravel 5.2 and your route is in the Web middleware group? csrf_token(); should work then. Make sure the following class are present in the middleware group in your Kernel:

protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
        ],
    ];

Yes, I am using same code

What happens when you try out Dan's test routes? Does /bar give you an empty page as well?

Only Blank screen, Nothing happend

me too ,just return null !! why?

Both get and post for that view should be under web middleware

Was this page helpful?
0 / 5 - 0 ratings