我试图弄清楚为什么这个错误,即使它的新安装。我在我的项目中得到这个错误所以我用Google搜索,没有任何答案对我有效。所以我创建了新项目并复制了所有控制器,视图和model.its在几个小时之后再次出现令牌不匹配错误。为什么这种情况发生在laravel?
我的表格
<form class="form-horizontal action="http://localhost/laravel/public/add-post-new" enctype="multipart/form-data" method="POST" accept-charset="UTF-8" >
<div class="form-group">
<label for="inputEmail" class="control-label col-xs-2">Title</label>
<div class="col-xs-10">
<input type="text" class="form-control" id="post_title" placeholder="Title" name="post_title">
</div>
</div>
<div class="form-group">
<label for="inputPassword" class="control-label col-xs-2">Content</label>
<div class="col-xs-10"><textarea class="form-control" style="resize:none" rows="25" name="post_content"></textarea>
</div>
</div>
<div class="form-group">
<label for="inputPassword" class="control-label col-xs-2">Featured Image</label>
<div class="col-xs-10"><input type="file" class="filestyle" data-buttonText="Find" name="featured_image">
</div>
</div>
<div class="form-group">
<label for="inputPassword" class="control-label col-xs-2">Post Images</label>
<div class="col-xs-10"><input type="file" class="filestyle" name="post_gallery[]" multiple />
</div>
</div>
<div class="form-group">
<label for="inputPassword" class="control-label col-xs-2">Select Category</label>
<div class="col-xs-10" >
<select class="form-control" name="cat_id">
<?php $data=Category::all(); ?>
<option value="0">Default Category</option>
@foreach($data as $value)
<option value="{{$value->id}}">{{$value->cat_name}}</option>
@endforeach
</select>
</div>
</div>
<div class="form-group">
<label for="inputPassword" class="control-label col-xs-2">Publish Post</label>
<div class="col-xs-10">
<label class="radio-inline">
<input type="radio" name="published" id="inlineRadio1" value="1"> Publish
</label><br>
<label class="radio-inline">
<input type="radio" name="published" id="inlineRadio2" value="2"> UnPublish
</label><br>
<label class="radio-inline">
<input type="radio" name="published" id="inlineRadio2" value="3"> Draft
</label><br><br>
</div>
</div>
<div class="form-group">
<label for="inputPassword" class="control-label col-xs-2">Slider Post</label>
<div class="col-xs-10">
<label class="radio-inline">
<input type="radio" name="slider_post" id="inlineRadio1" value="1"> Slider Post
</label><br>
<label class="radio-inline">
<input type="radio" name="slider_post" id="inlineRadio2" value="2"> Not required
</label><br><br>
</div>
</div>
<div class="form-group">
<div class="col-xs-offset-2 col-xs-10">
<input name="_token" type="hidden" value="{{ csrf_token() }}"/>
<button type="submit" class="btn btn-primary">Submit Post</button>
</div>
</div>
在提问之前我已经阅读了很多教程
Laravel 5, Forms, TokenMismatchException in VerifyCsrfToken.php line 46
Laravel 5 Auth Post Submit - TokenMismatchException in VerifyCsrfToken.php line 46
TokenMismatchException in VerifyCsrfToken.php line 53 in Laravel 5.1
TokenMismatchException in VerifyCsrfToken.php line 46
Laravel 5, ajax, 500 Internal Server Error, TokenMismatchException in VerifyCsrfToken.php line 46:
Laravel 5 TokenMismatchException in VerifyCsrfToken.php line 46
Encountering "TokenMismatchException in VerifyCsrfToken.php" error
Laravel TokenMismatchException
http://laravel.io/forum/01-30-2015-laravel5-tokenmismatchexception-in-verifycsrftoken
答案 0 :(得分:3)
这可能对某人有所帮助。 检查不以空行或空格开头的php文件! 这花了我很多麻烦。包括以上内容!
答案 1 :(得分:2)
突然间我得到了这个例外。
然后重启并清理缓存对我有用。
清除缓存使用情况:php artisan cache:clear
答案 2 :(得分:1)
尝试在打开表单后添加此行
<input type="hidden" name="_token" value="{{ csrf_token()}}"/>
答案 3 :(得分:1)
我遇到了这个痛苦的错误,这就是我所做的修复它:
1.转到\Http\Controllers\Middleware\VerifyCsrfToken.php
2.在protected $except
添加您的路线,以便从此验证中排除。示例:
protected $except = [
'user*'
];
如果仍然出现此错误,请将此功能添加到受保护的$除
之外public function handle($request, Closure $next) {
$regex = '#' . implode('|', $this->except) . '#';
if ($this->isReading($request) || $this->tokensMatch($request) || preg_match($regex, $request->path())) {
return $this->addCookieToResponse($request, $next($request));
}
throw new TokenMismatchException;
}
我认为就是这样。我希望能帮助别人。