Home  >  Article  >  Backend Development  >  laravel session method is useless

laravel session method is useless

WBOY
WBOYOriginal
2016-08-04 09:20:31859browse

Why doesn’t the session method work?

Reply content:

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>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn