N
N
Nik2016-08-26 14:54:51
Laravel
Nik, 2016-08-26 14:54:51

Why is csrf_token() empty on Laravel 5.2?

Hello!
I don’t understand why, when trying to create a form, csrf_token is empty?
I am using Laravel 5.2.44
Form:

{!! Form::open((array('action' => '[email protected]'))) !!}
    <input type="text" name="name" id="name">
    <button type="submit">Submit</button>
    {!! Form::close() !!}

The source code then looks like this:
<form method="POST" action="http://cms.local/contact" accept-charset="UTF-8"><input name="_token" type="hidden">
    <input type="text" name="name" id="name">
    <button type="submit">Submit</button>
    </form>

route:
Route::group(['middleware' => 'web'], function () {

    Route::post('contact', ['as' =>'contact','uses' => '[email protected]']);

    Route::get('bar', function () {
        return csrf_token(); // works
    });

});

Controller:
public function store(Request $request)
    {
        $input = Request::all();

        return $input;
    }

Surprisingly, if you go to /bar, then the token is normally generated. What could I have forgotten or where could I have gone wrong?
Thank you!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
Nik, 2016-08-26
@nikitinnv

Thanks to all who are not indifferent)))
I found a mistake. It turned out that I messed up in RouteServiceProvider and did not use web middleware for its class
Now I fixed it:

protected function mapWebRoutes(Router $router)
    {


        $router->group([
            'namespace' => $this->namespace, 'middleware' => 'web',
        ], function ($router) {
            require app_path('Http/routes.php');

            foreach (Page::all() as $page){
                $router->get($page->url, ['as' => $page->name, function () use ($page,$router){
                    return $this->app->call('LH\Http\Controllers\[email protected]',[
                        'page' => $page,
                        'parameters' => $router->current()->parameters()
                    ]);
                }]);
            }

        });
    }

I racked my brains for 2 days, but as soon as I published the question, I immediately found the answer ... What a setup)))

V
Vlad, 2016-08-26
@Result007

Hey!
Maybe it will help:
1) stackoverflow.com/questions/34422751/why-is-my-csr...
2) Why does csrf_token() return null on Laravel 5.2?

M
Mikhail Osher, 2016-08-26
@miraage

{{ csrf_field() }}lost in shape.
docs .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question