php - Laravel token 不匹配异常

标签 php laravel

现在我正在学习 laravel,但我不断得到异常(exception):

TokenMismatchException in VerifyCsrfToken.php line 53:

我正在尝试制作一个迁移对象,然后将其写入数据库,但由于某种原因它无法正常工作。这是我的 route.php:

Route::get('/post/new',array(
'uses'=> 'blog@newPost',
'as' => 'newPost'
    ));
Route::post('/post/new', array (
'uses' => 'blog@createPost',
'as' => 'createPost'
    ));

这是我的名为 blog.php 的 Controller :

使用 Illuminate\Http\Request;

use App\Http\Requests;
use View;
use App\Http\Controllers\Controller;
use App\posts;

    class blog extends Controller
    {
       public function newPost() 
       {
          return View::make('new');
       }

       public function createPost() 
       {
            $posts = new posts();
            $posts->title = Input::get('title');
            $posts->content = nl2br(Input::get('content'));
            $posts->save();
       }
    }

这是迁移:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts',function($table) {
            $table->increments('id');
            $table->string('title');
            $table->text('content');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        schema::drop('posts');
    }
}

这是我的主要观点:

@extends('master')
@section('content')
<h3>Add a blog post</h2>
 <form action="{{ URL::route('createPost') }}" method="post">
 <div class="form-group">
 <input name="title" class="form-control" type="text" placeholder="title"/>
 </div>
 <div class="form-group">
 <textarea name="content" class="form-control" placeholder="write here"> </textarea>
 </div>
 <input type="submit" class="btn btn-primary" />
 </form>
@stop

有什么问题吗?

最佳答案

</form> 之前添加这个

{!! csrf_field() !!}

看看Laravel docs了解更多信息

关于php - Laravel token 不匹配异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31427840/

相关文章:

php - MySql 空结果集返回不同格式

php - http.so 已加载但 http_get 未定义

PHP随机概率

Javascript - 接收和格式化 HTML

php - 为什么 float 在 laravel dompdf 中不起作用?

laravel - 如何解决laravel中的这个错误: ErrorException (E_WARNING) Invalid argument supplied for foreach()

php - 'where 子句中的未知列 "has"

php - MySQL/PHP mysql_fetch_array() 始终丢失第一行

laravel - 我应该在 Laravel Horizo​​n 中使用多少个进程?

php - str_replace() 与 foreach、大写和精确单词匹配