Home > PHP Framework > Laravel > body text

Detailed explanation of Laravel's method of handling session (session)

不言
Release: 2020-07-13 16:41:28
Original
10881 people have browsed it

In web applications, it is necessary to identify users across requests and save data for each user, and for this, frameworks like Laravel provide a mechanism called sessions. This article will introduce to you how Laravel handles sessions.

Detailed explanation of Laravels method of handling session (session)

Sessions can store data (keys and values), Laravel provides various backend sessions, which can be set in config/session.php.

File sessions by default save sessions in files in the storage/framework/sessions/ directory. In a production environment we will consider using database sessions, redis sessions, etc., but in a development environment it is enough to use the default file session.

php Chinese website learning topic: php session (including pictures, texts, videos, cases)

How to use sessions in Laravel

There are two main ways to operate sessions with Laravel.

One is through the Request instance passed to the operation.

Use Illuminate\Session\Store instance.

// 从会话中获取指定的数据
//在没有存在键的情况下,将返回的默认值指定为第二参数
$value = $request->session()->get('key’);
$value = $request->session()->get('key', 'default’);
$value = $request->session()->get('key', function () {
    return 'default';
});

// 获取会话中的所有数据
$data = $request->session()->all();

// 检查指定的数据是否存在于会话中
if ($request->session()->exists('key')) {
    // 存在
}
if ($request->session()->has('key')) {
    // null不存在
}

// 将数据保存到会话
$request->session()->put('key', 'value');
$request->session()->put(['key1' => 'value1', 'key2' => ‘value2']);

// 从会话取得指定的数据后,删除该数据
$value = $request->session()->pull('key', 'default’);

// 从会话中删除指定的数据
$request->session()->forget('key');

// 从会话中删除所有数据
$request->session()->flush();
Copy after login

Another way is to use the global helper function session().
Use Illuminate\Session\SessionManager instance.

// 从会话中获取指定的数据
// 在没有存在键的情况下,将返回的默认值指定为第二参数
$value = session('key’);
$value = session('key', 'default');
$value = session('key', function () {
    return 'default';
});
$value = session()->get(‘key');
$value = session()->get('key', 'default');
$value = session()->get('key', function () {
    return 'default';
});

// 取得会话中的全部数据
$data = session()->all();

// 检查指定的数据是否存在于会话中
if (session()->exists('key')) {
    // 存在
}
if (session()->has('key')) {
    // null不存在
}

// 保存数据到会话
session(['key1' => 'value1', 'key2' => ‘value2']);
session()->put(['key1' => 'value1', 'key2' => 'value2']);

// 从会话取得指定的数据后,删除该数据
$value = session()->pull('key', 'default’);

// 从会话中删除指定的数据
session()->forget('key');

// 从会话中删除所有数据
session()->flush();
Copy after login

Let’s look at A specific example of using session in Laravel

A very simple example

We define the following route.

routes/web.php

Route::get('/put-data', function () {
    session()->put(['email' => 'user@example.com']);
    return session()->get('email');
});

Route::get(‘/list-data', function () {
    return session()->all();
});
Copy after login

First, when accessing /put-data in the browser, the first path is executed and you can confirm that the data has been saved in the session.

user@example.com
Copy after login

Next, when accessing /list-data in the browser, the second path will be executed, and you can confirm whether the previously saved data has been retained

{"email":"user@example.com","_previous":{"url":"http:\/\/localhost:8000\/put-data"},"_flash":{"old":[],"new":[]},"_token":"UYcsteOQAj58e9Aay5uNc3V4F0fSpi9VfEBlKhTZ"}
Copy after login

Of course there are other data, But these are automatically saved data, Laravel itself also uses sessions.

The above is the detailed content of Detailed explanation of Laravel's method of handling session (session). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template