我在Laravel 4.2中得到了这个TokenMismatchException。当我试图发布请求时,TokenMismatchException会出现。例如登录页面。如果我提交该表单,就会显示TokenMismatchException。有什么方法可以验证所有提交的帖子请求吗
错误如下:
Route::filter('csrf', function()
{
if (Session::token() != Input::get('_token'))
{
throw new Illuminate'Session'TokenMismatchException;
}
});
这是我的代码:
route.php
Route::get('login',array('as' => 'login', 'uses' => 'AuthController@getLogin'))->before('guest');
Route::post('login',array('uses' => 'AuthController@postLogin'))->before('csrf');
Route::group(array('before' => 'auth'), function(){
Route::get('dashboard', array('as' => 'panel', 'uses' => 'DashboardController@view_dashboard'));
});
AuthController.php
class AuthController extends Controller {
public function getLogin(){
return View::make('users.login');
}
public function postLogin(){
$rules = array('username' => 'required', 'password' => 'required');
$validator = Validator::make(Input::all(), $rules);
if($validator->fails()){
return Redirect::route('login')->withErrors($validator);
}
$auth = Auth::attempt(array(
'username' => Input::get('username'),
'password' => Input::get('password')
), false);
if(!$auth){
return Redirect::route('login')->withErrors(array(
'Maaf anda bukan sebagai admin..'
));
}
//return "Success";
return Redirect::to('dashboard');
}
}
DashboardController.php
class DashboardController extends Controller {
public function view_dashboard(){
return View::make('dashboard.view_home_admin');
}
}
表单中似乎没有隐藏的csrf字段。
试着把这个添加到你的表单中
{{ Form::token() }}