在Laravel 5.1的VerifyCsrfToken.php第53行中没有TokenMismatchException的解决方案

时间:2015-08-27 02:12:39

标签: laravel-5.1

我是laravel框架版本5.1.10的新手。最近我试图在数据库中为CRUD做一个简单的项目。我正在尝试将数据从视图发送到控制器。但我得到这个错误。我尝试从stack和laravel.io/forum找到我的解决方案但失败了。这就是我需要你的建议的原因。 我的routes.php代码在

之下
<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

Route::get('/', function () {
    return view('welcome');
});
Route::get('/index','all_control@index');
Route::post('/insert_students','all_control@insert_students');

Route::get('/form',array(
    'as' => 'getform',
    'uses' => 'all_control@getform'
));

Route::post('/form',array(
    'as' => 'postform',
    'uses' => 'all_control@postform'
));

和我的form.blade.php在下面给出

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Title</title>
        <meta charset="UTF-8">
        <meta name=description content="">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="csrf-token" content="{{ csrf_token() }}">
        <!-- Bootstrap CSS -->
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" media="screen">
    </head>
    <body>
    <div class="container">
        <h1>Form</h1>
        <h3>
            <?php
                if(Session::has('message'))
                    {
                        echo Session::get('message');
                    }
            ?>
        </h3>
        <form action="{{ URL::route('postform') }}" method="post" role="form">
            <legend>Form Title</legend>

            <div class="form-group">
                <label for="">Roll</label>
                <input type="text" class="form-control" name="roll" id="roll" placeholder="Input...">
            </div>

            <div class="form-group">
                <label for="">Marks of Physisc CT</label>
                <input type="number" class="form-control" name="ct" id="ct" placeholder="Input...">
            </div>

            <button type="submit" class="btn btn-primary">Hit the Button</button>
        </form>
    </div>


    </body>
</html>

我的all_control.php就在这里

<?php

namespace App\Http\Controllers;

use App\Model\students;
use Illuminate\Http\Request;

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

class all_control extends Controller
{


    public function getform()
    {
        return view('form');
    }
    public function postform()
    {
        $roll = Input::get('roll');
        $ct_number = Input::get('ct');
        Session::flash('message','You have done successfully!!!!');
        return Redirect::route('getform');
    }
}

2 个答案:

答案 0 :(得分:2)

只需添加此

<input type="hidden" name="_token" value="{{ csrf_token(); }}">

打开表格后立即

<form action="{{ URL::route('postform') }}" method="post" role="form">
   <input type="hidden" name="_token" value="{{ csrf_token(); }}">
   <legend>Form Title</legend>

   <div class="form-group">
      <label for="">Roll</label>
      <input type="text" class="form-control" name="roll" id="roll" placeholder="Input...">
   </div>
   <div class="form-group">
       <label for="">Marks of Physisc CT</label>
       <input type="number" class="form-control" name="ct" id="ct" placeholder="Input...">
   </div>
   <button type="submit" class="btn btn-primary">Hit the Button</button>
</form>

答案 1 :(得分:0)

我上周也遇到过这个错误,但是当我现在切换到ajax帖子时它运行正常我不知道在php帖子中你可以尝试:

//必须在head标签

<meta name="csrf-token" content="{{ csrf_token() }}">

<script type=text/javascript>
$.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
});
</script>