仍然有错误' TokenMismatchException'在使用post方法提交表单时,在laravel V5.4中:

时间:2017-07-18 13:22:11

标签: php laravel laravel-5.4

我是laravel V5.4的初学者,我已经设置了基本配置。

我创建了一个注册表单,但是当在控制器中使用post方法提交表单时收到错误TokenMismatchException

提前致谢。

我有以下代码。

查看文件:

<form method="post" style="margin: 20% 40%;" action="{{ action('UserController@insert_record')}}">
        {{ csrf_field() }}
        <table>
            <tr>
                <td>First Name</td>

                <td><input type="text" name="FirstName" /></td>
            </tr>
            <tr>
                <td>Last Name</td>
                <td><input type="text" name="LastName" /></td>
            </tr>
            <tr>
                <td>Gender</td>
                <td>
                    <input type="radio" name="Gender" checked="checked" value="Male"/>
                    <input type="radio" name="Gender" value="Female"/>
                </td>
            </tr>
            <tr>
                <td><input type="submit" value="Insert" /></td>
            </tr>
        </table>

    </form>

路线/ Web.php

Route::match(['get', 'post'],'/insert_employer','UserController@insert_record');

应用程序/ HTTP / Middelware / VerifyCsrfToken.php

<?php

 namespace App\Http\Middleware;

 use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;

 class VerifyCsrfToken extends BaseVerifier
{
  /**
  * The URIs that should be excluded from CSRF verification.
  *
  * @var array
  */
   protected $except = [
    //
  ];




 }

UserController.php

<?php

 namespace App\Http\Controllers;

 use Illuminate\Http\Request;

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

class UserController extends Controller
{
   public function index(){
   $users = employers::all();
   return view('user_view',['users'=>$users]);
}

public function add_employer(){
   return view('add_employer');
}



  public function insert_record(){
   echo '<pre>';print_r($_GET); echo '<pre>';print_r($_POST);exit;

 }


 }

4 个答案:

答案 0 :(得分:1)

Laravel 5.4默认在中间件中强制执行CSFR令牌认证。如果您使用post方法提交表单,最好将每个路由放置在CRSF中间件而不是将其作为全局中间件放置。

/**
* The application's global HTTP middleware stack.
*
* @var array
*/
protected $middleware = [
  'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class',
  'Illuminate\Cookie\Middleware\EncryptCookies::class',
  'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class',
  'Illuminate\Session\Middleware\StartSession::class',
  'Illuminate\View\Middleware\ShareErrorsFromSession::class',
  //comment out to avoid CSRF Token mismatch error
  // '\App\Http\Middleware\VerifyCsrfToken::class',,
];

/**
* The application's route middleware.
*
* @var array
*/
protected $routeMiddleware = [
   'auth' => 'App\Http\Middleware\Authenticate::class',
   'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class',
   'guest' => 'App\Http\Middleware\RedirectIfAuthenticated::class',
   'cors' => 'App\Http\Middleware\CorsMiddleware::class',
   'api' => 'App\Http\Middleware\ApiMiddleware::class',
   'csrf' => 'App\Http\Middleware\VerifyCsrfToken::class'// add it as a middleware 
   route 

转到\Http\Controllers\Middleware\VerifyCsrfToken.php和 在protected $except中添加您要排除此验证的路线。例如:

protected $except = [
    'user*'
];

确保您的php文件在开始<?php标记之前不以空行或空格开头!这花了我很多麻烦。包括以上内容!

我认为就是这样。我希望它对你有用!!

答案 1 :(得分:0)

dd(request()->all());放在insert_record()

的开头时,您会得到什么?

答案 2 :(得分:0)

将csrf_token移到输入之外:

<form action="">
{{  csrf_token() }}
<table>
</table>
</form>

答案 3 :(得分:0)

试试这个:

<form>
  {!! csrf_field() !!}

  ...rest of the form body
</form>