laravel“csrf”问题“Illuminate \ Session \ TokenMismatchException”

时间:2015-01-05 12:45:00

标签: php laravel

在Laravel中,我得到了一个" csrf"问题" Illuminate \ Session \ TokenMismatchException"

Route::group(array('before'=>'guest'),function()
{
    Route::get('/user/create',array('uses'=>'UserController@getCreate'));
    Route::get('/user/login',array('uses'=>'UserController@getLogin','as'=>'postCreate'));

    Route::group(array('before'=>'csrf'),function()
    {   
        Route::get('/user/create','UserController@postCreate');
        Route::get('/user/login','UserController@postLogin');
    });
});

是控制器

class UserController extends BaseController{
public function getCreate()
{
    //return View::make('hello');
    return View::make('user.register');
}

即视图

<div class="container">
<h1>Register</h1>
<form role="form" method="post" action="{{ URL::route('postCreate')}}">
    <div class="form-gourp">
        <label for="username">Username: </label>
        <input id="username" name="username" type="text" class="form-control" />
    </div>
    <div class="form-gourp">
        <label for="password">Password: </label>
        <input id="password" name="password" type="text" class="form-control" />
    </div>
    <div class="form-gourp">
        <label for="username">confirm Password: </label>
        <input id="cpassword" name="cpassword" type="text" class="form-control" />
    </div>
    {{form::token()}}
    <div class="form-gourp">
         <input type="submit" value="register" class="btn btn-default"/>
    </div>
</form>

当我添加{{form::token }}时,它会显示错误&#34; Illuminate \ Session \ TokenMismatchException&#34;

2 个答案:

答案 0 :(得分:3)

你的路线错了。

Route::group(array('before'=>'csrf'),function()
{   
    Route::get('/user/create','UserController@postCreate');
    Route::get('/user/login','UserController@postLogin');
});

那些应该是post,如下所示

Route::group(array('before'=>'csrf'),function()
{   
    Route::post('/user/create','UserController@postCreate');
    Route::post('/user/login','UserController@postLogin');
});

wikilaravel doc上详细了解CSRF。

答案 1 :(得分:1)

使用{{ Form::open() }}{{ Form::close() }}而不是<form>代码