Using CSRF in Laravel

旧时模样 提交于 2019-12-25 07:03:37

问题


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:


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!';
    }

}


来源:https://stackoverflow.com/questions/26749786/using-csrf-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!