0
1

More than 3 years have passed since last update.

LaravelでどうしてもSESSIONが追加できないときにすること

Last updated at Posted at 2020-06-02

こんなにもシンプルなSESSIONが追加できない

app/Http/Controllers/HogeController.php
  public function show()
  {
    if (!Session::has('token')) {
      Session::put('token', 'token!');
      var_dump('saved!');
    }
    var_dump(Session::get('token'));
    return view('welcome');
  }

初回のみ「saved!token!」と表示され、2回目以降は「token!」となってほしい。
しかし、何度試しても「saved!token!」が表示される。

不用意にダンプしたらダメなんだってさ

https://laracasts.com/discuss/channels/laravel/sessions-are-not-saving?page=1

sessions work fine, but only if your code exits normally and runs the terminable middleware. If you dd() in your code then the session is not saved

https://laravel.com/docs/5.5/middleware#terminable-middleware

you should also run php artisan route:list and check that you have web against each route, once only per route

テストコードのvar_dumpを削除したらセッションにちゃんと保存できた。

app/Http/Controllers/HogeController.php
  public function show()
  {
    if (!Session::has('token')) {
      Session::put('token', token!);
    }
    return view('welcome');
  }
0
1
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
0
1