The introduction
We know that HTTP requests are stateless and that there is no direct relationship between the two requests. In most cases, however, we need to maintain data continuity between user sessions, and it is necessary to temporarily store context data on the server for data security purposes.
That’s what sessions are designed for.
Code time
In Laravel, Session data can be easily manipulated using the system-provided Session class, and the storage medium is abstracted and seamless, just by changing the driver.
Session drivers supported in the framework are as follows:
- The file is stored in the storage/framework/sessions
- Cookies are stored in securely encrypted cookies
- Database Creates specialized tables and stores them in the database
- Memcached, redis stored in these in-memory databases
- Array is available on each request and burns out, making it suitable for testing
Session data is stored in a simple key-value format, so reading the value of a key name only requires
session()->get('user_id');
Copy the code
The session() function is a helper function provided by the Laravel system. Let’s look at the source definition:
function session($key = null.$default = null)
{
if (is_null($key)) {
return app('session');
}
if (is_array($key)) {
return app('session')->put($key);
}
return app('session')->get($key.$default);
}
Copy the code
If the key name is empty, return an app(‘session’) and use the application container to instantiate a session object. The alias session is registered inside Illuminate\Support\Facades\ session.
Because sessions are held between page requests, the Request object also provides a session method. Within the FormRequestServiceProvider registration request body calls when initialization method:
$request = FormRequest::createFrom($app['request'].$request);
Copy the code
The createFrom method writes session data:
if ($session = $from->getSession()) {
$request->setLaravelSession($session);
}
Copy the code
So with that in mind, we can get the session value of a request as follows:
Route::get('dashboard'.function (Request $request) {
$request->session()->get('user_id');
});
Copy the code
Or use dependency injection to read data directly from storage:
Route::get('dashboard'.function (Illuminate\Session\Store $session) {
return $session->get('user_id');
});
Copy the code
To take advantage of the flexibility of PHP, the session helper function also provides a complete request cache and other operations to read and write data. For example, reading the value of a key:
$value = session()->get('key');
$value = session('key');
Copy the code
Also write values:
session()->put('key'.'value');
session(['key'.'value']);
Copy the code
Use skills
Here are some of the ways to store session data and the methods of classes provided by the framework. For example, get the value directly from the key name:
$points = session()->get('points');
Copy the code
If the key name does not exist, use the default value:
$points = session()->get('points'.0);
Copy the code
Take a look at the definition of the get method and we’ll learn more flexible uses:
public function get($key.$default = null)
{
return Arr::get($this->attributes, $key.$default);
}
Copy the code
Note on the variable type hint that $default is mixed generic. Then look at the use of the Arr::get() method:
if (! static::accessible($array)) {
return value($default);
}
Copy the code
If the **$this->attributes** passed is an array, use the value() helper function to value it. Take a look at the definition of the helper function:
function value($value)
{
return $value instanceof Closure ? $value() : $value;
}
Copy the code
We found that if an anonymous function is passed in, the call is made directly and the value is returned after execution. Thus, we can further modify the second default of the session()->get() method above, passing it into an anonymous function that handles the logic to get the default value.
Look at this code:
$points = session()->get('points'.function () {
return (new PointGetterService)->getPoints();
});
Copy the code
-) With the support of anonymous functions, this is the default value you can play with
Write in the last
This article introduces laravel’s method of keeping data between requests: session. The session ::get() method is introduced in the program, and in-depth source code to explore the high-level use of the session ::get() method.
Happy coding 🙂
I am @programmer assistant, continue to share programming knowledge, welcome to follow.