VerifyCsrfToken.php第67行中的错误TokenMismatchException:

时间:2017-05-27 08:43:24

标签: php laravel-5 laravel-4 laravel-5.2 laravel-5.3

我是一个新的laravel。我尝试在数据库中创建类别,但我遇到了VerifyCsrfToken.php第67行中的TokenMismatchException问题:遇到这个问题后,我尝试将{{csrf_field()}}添加到我的表单中,但它得到了同样的错误。请帮我解决这个问题。感谢

查看表单

{!! Form::open(array('action' => ['Page_Admin@updatemaincate', $main_cate->id], 'method' => 'PUT', 'enctype' => 'multipart/form-date')); !!}
    {{ csrf_field() }}
    <div class="modal-header" style="background:#3C8DBC;">
        <button type="button" class="close"  data-dismiss="modal">&times;</button>
        <h4 class="modal-title" style="color:#fff;">New Main-Category</h4>
    </div>
    <div class="modal-body">
        <div class="row">                               
            <div class="col-md-12">
                <div class="col-lg-12 col-md-12 col-sm-12">
                    <div class="form-group">
                        {!! Form::label('title', 'Title'); !!}
                        {!! Form::text('title', $value = $main_cate->title, $attributes = ['class' => 'form-control', 'name' => 'title']); !!}
                    </div>
                </div>
            </div> <!--end 12-->
        </div> <!--end row-->
    </div>
    <div class="modal-footer custom-default">
        {!! Form::submit('Edit', $attributes = ['class' => 'btn btn-default']) !!}
        <button type="button" class="btn btn-default" data-dismiss="modal">No</button>
    </div>
{!! Form::close(); !!} 

控制器

public function maincategories()
{
    $maincategory = maincategory::all();
    return view('admin/maincategories', compact('maincategory'));
}

public function setmaincate(Request $request){
    $this->validate($request,[
        'title' => 'required'
    ]);

    $tbl_maincate = new maincategory;
    $tbl_maincate->title = Input::get('title');
    $tbl_maincate->save();
    Session::flash('success', 'Adding multiple images are successfully');
    return Redirect('/maincategories');
}

路线

Route::get('/', 'Page_Admin@index');
Route::resource('admin', 'Page_Admin');
Route::get('maincategories', 'Page_Admin@maincategories');
Route::post('setmaincate', 'Page_Admin@setmaincate');

2 个答案:

答案 0 :(得分:0)

表单方法必须是“POST”,您可以使用{{method_field('PUT')}}来设置方法

答案 1 :(得分:0)

app/Exceptions/Handler.php中的

将以下代码放入render函数

//instance of Token Mismatched
    if ($exception instanceof TokenMismatchException){
        //redirect to a form. Here is an example of how I handle mine
        return redirect($request->fullUrl())->with('csrf_error', $exception->getMessage());
    }

在顶部使用以下行:

use Illuminate\Session\TokenMismatchException;