In Laravel, you can use the session's get, put, pull, set, has, Flash and other methods to operate, such as: Session::p ut (' domain ' => ' tanteng.me ', ' Host ' => ' a Liyun ']), you can set two session values to see the Put method:
Code is located in vendor/laravel/framework/src/illuminate/session/store.phpphp
/**
* Put a Key/value pair or array of Key/value pairs during session.
*
* @param string|array $key
* @param mixed $value
* @return void
*/
The public function is put ($key, $value = null)
{
if (! Is_array ($key)) {
$key = [$key => $value];
}
foreach ($key as $arrayKey => $arrayValue) {
$this->set ($arrayKey, $arrayValue);
}
}
Code is located in vendor/laravel/framework/src/illuminate/session/store.php
It actually sets the session value through the Set method, set method:
/**
* {@inheritdoc}
*/
Public function set ($name, $value)
{
Arr::set ($this->attributes, $name, $value);
}
The visible set method simply saves the value in the property attributes of the store class, where you save the session by saving the value in one place, not actually setting it, but saving it for the last time, save the session by the store class SA ve method.
Where the Save method:
/**
* {@inheritdoc}
*/
Public Function Save ()
{
$this->addbagdatatosession ();
$this->ageflashdata ();
$this->handler->write ($this->getid (), $this->prepareforstorage (Serialize ($this->attributes)));
$this->started = false;
}
So where do I call the Save method to set the session value?
Laravel Middleware Startsession
Terminate middleware is used here, startsession this middleware has a Terminate method, as follows:
/**
* Perform any final actions for the request lifecycle.
*
* @param \illuminate\http\request $request
* @param \symfony\component\httpfoundation\response $response
* @return void
*/
Public function Terminate ($request, $response)
{
if ($this->sessionhandled && $this->sessionconfigured () &&! $this->usingcookiesessions ()) {
$this->manager->driver ()->save ();
}
}
Here we only talk about the Terminate method of Startsession, where $this->manager->driver ()->save () is called to save the set session value by invoking the Save method previously spoken.
What is terminable middleware
Here's a quote from somewhere else:
Sometimes the middleware needs to do something after the HTTP response has been sent to the browser. For example, this middleware, which is included in Laravel, writes session data to the store after the response is sent to the browser. To implement this function, you can define the middleware as terminable.
The original is in response to do things, save the session is a one-time step in the last, this is the laravel session to save the mechanism, as well as the use of terminate middleware.
The Laravel session provides multiple storage modes, file,memcache,redis,database, and so on, which can be set through configuration files.