LoginSignup
17
14

More than 3 years have passed since last update.

LaravelでPOSTが失敗する

Last updated at Posted at 2017-12-07

Laravelでformをポストしたら以下のエラーがでた

The page has expired due to inactivity.
Please refresh and try again.

対応策

csrf対策をする

対応案

公式ドキュメントによると、以下のようにすれば良いとのこと

<form method="POST" action="/profile">
    {{ csrf_field() }}
    ...
</form>
https://readouble.com/laravel/5.3/ja/csrf.html

まだ、サンプルプロジェクトで、ヒアドキュメントでHTMLを直接書いていたので、
以下のように対応

$csrf = csrf_field()

$html = <<<EOF
<form method="POST" action="/profile">
    {$csrf}
    ...
</form>
EOF
return $html;

すると、html中には以下のようなhiddenフィールドが挿入されていた。

これによりエラーは解消した。

17
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
14