いつもお世話になっております。
目的:sessionを取得して、$idにて値を取得する。($idはpublicにてnullで設定されています。)
php
1PublicController.php 2const SESSION_KEY = 'aa'; 3const TYPE_KEY = 'a_type'; 4const STAGE_KEY = 'a_stage'; 5 6public $id = null; 7 8public function __construct() 9{ 10 $this->beforeFilter(function($route, $request) 11 { 12 $session = $request->session()->get(self::SESSION_KEY); 13 if ($session) { 14 $this->id = (int)$session; 15 } elseif(($token = $request->cookie(self::SESSION_KEY)) && (($id = Cookie::findByToken($token, self::SESSION_KEY)) !== NULL)) { 16 $this->id = (int)$id; 17 $request->session()->put(self::SESSION_KEY, (int)$id); 18 } 19 }); 20 }
php
1Models/Cookie.php 2public static function findByToken($token, $type) { 3 $cookie = self::select('id', 'type', 'uid')->where('token', $token)->first(); 4 if (!$cookie) { 5 \Cookie::forget($type); 6 return null; 7 } 8 if (!starts_with($cookie['type'], $type)) { 9 self::where('id', $cookie['id'])->delete(); 10 \Cookie::forget($type); 11 return null; 12 } 13 $type = substr($cookie['type'], strlen($type)); 14 if ($type) { 15 return [ 16 'type' => substr($type, 1), 17 'id' => $cookie['uid'], 18 ]; 19 } else { 20 return $cookie['uid']; 21 } 22}
Controller.php public function beforeFilter($filter, array $options = []) { $this->beforeFilters[] = $this->parseFilter($filter, $options); }
上記にて、$sessionがnullのため$idでnullとなってしまっているのだと考えております。
$sessionと$idのそれぞれでnullではない値が取得できるようにしたいのですが、方法がわかっておりません。
「やったこと」
ブラウザのcookieを有効にする。
使っているxamppのsession.auto_startをONにする。
ご教授のほどよろしくお願いいたします。
eclipseを使用しているのですが、beforeFilterに取り消し線が引かれています。これが原因でnullが返ってきてしまっているのでしょうか?
※追記
set-cookieが消えてしまっているのを確認いたしました。
原因は不明ですが、どのようにすれば再度set-cookieを作成できますでしょうか?