Why doesn’t the session method work?
Why doesn’t the session method work?
<code>use Illuminate\Support\Facades\Session; Session::put('key', 'value'); $value = Session::get('key'); echo $value;</code>
Let me tell you the reason. Laravel's session mechanism is only stored uniformly after your class method is executed. That is to say, after you call session([foo,bar]);
, it will not be directly stored in the session. Instead, it will be stored after the entire logic execution is completed. If you need to access session information in real time, you can add a Session::save();
and it will be stored.
There is another situation, that is, if your code has a forced exit such as exit, etc., or there is an internal code error, the previously set session value cannot be stored.
Don’t thank me, I just stayed in the pit for too long, /(ㄒoㄒ)/~~
The posture you are using is wrong, please refer to [Laravel 5.2 Documentation] Service——Session
<code> // 从session中获取数据... $value = session('key'); // 存储数据到session... session(['key' => 'value']);</code>