【发布时间】:2016-10-07 10:27:50
【问题描述】:
当我尝试在 laravel 中保存表单中的数据时,总是会出现此错误:
VerifyCsrfToken.php 第 68 行中的 TokenMismatchException:
但是当我通过 apache 访问 laravel 时出现错误,当我通过命令 php artisan serve --host 0.0.0.0 运行 laravel 服务器时,它运行良好...
这是我的表单视图:
<form class="" method="POST" action="{{ $card->path() }}/notes">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<textarea name="body" class="form-control" rows="8" cols="40"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary" name="button">Dodaj</button>
</div>
</form>
这是我的控制器:
<?php
namespace App\Http\Controllers;
use App\Card;
use App\Note;
use Illuminate\Http\Request;
use App\Http\Requests;
class NotesController extends Controller
{
public function store(Request $request, Card $card)
{
$card->notes()->save(
new Note(['body' => $request->body])
);
return back();
}
}
这是我的函数,它会在成功发送表单到数据库后重定向用户:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Card extends Model
{
//
public function notes()
{
return $this->hasMany(Note::class);
}
public function path()
{
return '/cards/' . $this->id;
}
}
【问题讨论】:
-
你能显示你的路线吗?
-
Route::get('cards/{card}', 'CardsController@show');Route::post('cards/{card}/notes', 'NotesController@store');这里 -
你把它放在你的网络路由组里了吗?
-
是的,这是在
routes/web.php