2017-05-27 126 views
0

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

观形成

{!! Form::open(array('action' => ['[email protected]', $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('/', '[email protected]'); 
Route::resource('admin', 'Page_Admin'); 
Route::get('maincategories', '[email protected]'); 
Route::post('setmaincate', '[email protected]'); 
+0

你能检查你的路由是否使用'web'中间件组?你可以运行'php artisan route:list'并检查中间的列。您的路线应该使用“网络”中间件组进行工作。 – Sandeesh

回答

0

形式方法必须是 “POST”,您可以使用{{method_field( 'PUT')}}设置方法在你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()); 
    } 

在顶部使用以下行

+0

谢谢你的建议,但我仍然有同样的问题 –

0

use Illuminate\Session\TokenMismatchException; 
+0

谢谢你的建议,但我仍然有同样的问题 –

+0

清除你的工匠缓存'php artisan config:cache' –

+0

谢谢!我已经清楚,但仍然有同样的问题 –