【发布时间】:2015-12-02 14:20:31
【问题描述】:
我试图在请求验证后在数据库中插入值,但它返回了 TokenMismatchException。我不明白为什么?
HTML 表单
{!! Form::open(['url' => 'admin/movie', 'id' => 'tab', 'files' => true]) !!}
<div class="form-group">
<label><strong>Movie Name</strong></label>
<input type="text" name="name" value="{{ old('name') }}" class="form-control">
</div>
<div class="form-group">
<label><strong>Movie Poseter</strong></label>
<input type="file" name="poster" class="form-control">
</div>
<div class="form-group">
<label><strong>Description</strong></label>
<textarea name="description" rows="3" class="form-control">{{ old('description') }}</textarea>
</div>
<div class="btn-toolbar list-toolbar">
<button class="btn btn-primary"><i class="fa fa-save"></i> Save</button>
<button class="btn btn-danger"><i class="fa fa-ban"></i> Cancel</button>
</div>
{!! Form::close() !!}
路线
Route::resource('admin/movie', 'MovieController');
控制器
class MovieController extends Controller
{
/**
* Create a new movie model instance.
*
* @return void
*/
public function __construct(MovieModel $movie){
$this->movie = $movie;
}
public function store(MovieRequest $request)
{
$input = array_except(Input::all(), '_token');
$destinationPath = public_path() . '/' . 'posters/';
$poster = $request->file('poster');
$poster_ext = $poster->guessExtension();
$poster_name = sha1($poster->getClientOriginalName() . time()) . '.' . $poster_ext;
if($poster->move($destinationPath, $poster_name)) {
$input['poster'] = $poster_name;
}
if($this->movie->createMovie($input)){
return Redirect::back()->with('message', $input['name'] . ' movie has been created');
}
}
}
型号
class Movie extends Model
{
public function createMovie($input) {
return $this->insert($input);
}
}
请求
class MovieRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|unique:movies|max:50',
'poster' => 'required',
'description' => 'required|max:1000'
];
}
}
我是 laravel 5 的新手,我之前使用过 laravel4。我不明白为什么会遇到这个问题。
【问题讨论】:
-
默认采用POST方式
-
在您的标头中发送您的 XSRF-Token。
-
@Uchiha 您的解决方案对我有用,请发表您的评论作为答案
标签: php laravel-5.1