Laravel session save mechanism and terminate middleware

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.