TokenMismatchException laravel 5.1

时间:2015-12-02 06:05:58

标签: php laravel-5.1

我试图在请求验证后在数据库中插入值,但它返回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合作过。我不知道为什么我会遇到这个问题。

1 个答案:

答案 0 :(得分:1)

您没有在method标记中定义form属性。您需要在methodGETPOSTPUT之上定义PATCH。因此,当您存储价值时,method将发布,以便更新您的代码

{!! Form::open(['url' => 'admin/movie', 'id' => 'tab', 'files' => true]) !!}

{!! Form::open(['url' => 'admin/movie', 'method' => 'POST' ,'id' => 'tab', 'files' => true]) !!}
                                       ^^^^^^^^^^^^^^^^^^^^