Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
315 views
in Technique[技术] by (71.8m points)

php - Laravel TokenMismatchException

Right now I'm learing laravel but I keep getting the exeption:

TokenMismatchException in VerifyCsrfToken.php line 53:

I'm trying to make an object of a migration and then write it to the database but for some reason it's not working. This is my route.php:

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

This is my controller called blog.php:

use IlluminateHttpRequest;

use AppHttpRequests;
use View;
use AppHttpControllersController;
use Appposts;

    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();
       }
    }

This is the migration:

<?php

use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;

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');
    }
}

And this is my main view:

@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

What could be wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Add this right before </form>

{!! csrf_field() !!}

Take a look at Laravel docs for more info


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...