Using CSRF in Laravel

2019-08-27 16:28发布

Here is my CSRF as hidden

<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">

And my csrf is generated as usual

While i am passing into route for a controller

Here is my old route

Route::post('register', 'RegisterController@registeruser');

And to make it with csrf

Route::post('register', array('before' => 'csrf', function()
{
    return 'You gave a valid CSRF token!';
}));

as per the Laravel Docs

While i routing it to the controller

Route::post('register', array('before' => 'csrf', RegisterController@registeruser()
{
    return 'You gave a valid CSRF token!';
}));

I am getting the error

syntax error, unexpected '{', expecting ')'

What is the mistake i am doing and how can i fix this ?

1条回答
劫难
2楼-- · 2019-08-27 16:36

Your route should be this:

Route::post('register', array('before' => 'csrf', 'uses' => 'RegisterController@registeruser');

Then you can handle it in your controller

class RegisterControllerextends Controller {

    protected function registeruser()
    {
        return 'You gave a valid CSRF token!';
    }

}
查看更多
登录 后发表回答