VerifyCsrfToken.php中的larval TokenMismatchException

时间:2016-06-28 12:54:11

标签: php laravel laravel-5

我正在使用幼虫5

这是我的HTML

<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
</head>
<body>
    <h3>This is a test page </h3>
    <form method="POST" action="./apartment/sayLove2">
        <input name="id" value="1" type="text"/>
        <input name="team" value="roma" type="text"/>
        <input name="bestPlayerInTheWorld" value="TOTTI" type="text"/>
        <input  value="Send" type="submit" />
    </form>
</body>
</html>

这是我的路线

Route::post("/apartment/sayLove2", 'ApartmentController@sayLove2');

这是我的控制器

<?php
namespace App\Http\Controllers;



use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;


class ApartmentController extends Controller
{
    public function sayLove2(Request $request)
    {
        $id = $request->input('id');
        echo $id;
        eixt;
        $team = $request->input('team');
        $bestPlayerInTheWorld = $request->input('bsetPlayerInTheWorld');
         return view('sayLove', ['id' => $id, $team => $team]);
    }


}
?>

点击提交表单

后出现此错误
TokenMismatchException in VerifyCsrfToken.php line 67:
in VerifyCsrfToken.php line 67
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 62
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Router.php line 726
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 699
at Router->dispatchToRoute(object(Request)) in Router.php line 675
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 54
at require_once('/Applications/XAMPP/xamppfiles/htdocs/ULTest/public/index.php') in server.php line 21

4 个答案:

答案 0 :(得分:3)

请看这里:https://laravel.com/docs/master/routing#csrf-protection

您需要添加CSRF-TOKEN。

您可以在表格中添加隐藏的输入,如下所示:

<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">

答案 1 :(得分:0)

如果所有配置正确,请尝试此操作。 我也遇到了这个问题,并遵循了太多建议,但最终,以下对我来说效果很好。

php artisan cache:clear

php artisan config:clear

php artisan config:cache

答案 2 :(得分:0)

每次在应用程序中定义HTML表单时,都应在表单中包含一个隐藏的CSRF令牌字段,以便CSRF保护中间件可以验证请求。

您可以使用@csrf Blade指令生成令牌字段:

<form method="POST" action="/someurl">
    @csrf
    ...
</form>

您可以在laravel文档站点上了解有关它的更多信息: https://laravel.com/docs/7.x/csrf#csrf-introduction

答案 3 :(得分:-1)

最短的答案:

{!! csrf_field() !!}

请参阅here