Laravel - Session
Laravel - 会话(Session)管理
Section titled “Laravel - 会话(Session)管理”会话(Sessions)是一种在多个请求之间存储用户信息的方式。Laravel 提供了各种驱动来处理会话数据,包括 file、cookie、database、apc、array、memcached 和 redis。默认使用 file 驱动,因为它轻量级且无需额外设置。会话配置位于 config/session.php 文件中。对于生产应用,通常首选 redis 或 memcached 等驱动以获得更好的性能。
访问会话数据
Section titled “访问会话数据”要访问会话数据,你通常使用 Request 对象,它可以被注入到你的控制器方法或路由闭包中。一旦有了 request 实例,你就可以访问 session 实例并使用 get() 方法。该方法接受一个参数,即会话的 key,以检索其值。如果找不到该 key,它会返回 null,或者如果提供了第二个参数,则返回该默认值。
// Via Illuminate\Http\Request instance$value = $request->session()->get('key');$valueWithDefault = $request->session()->get('key', 'default_value');
// You can also use the global session helper$value = session('key');$valueWithDefault = session('key', 'default_value');要检索会话中的所有数据,可以使用 all() 方法:
$allData = $request->session()->all();要检查会话中是否存在某个项,使用 has() 方法。如果该项存在且不为 null,则返回 true。
if ($request->session()->has('key')) { // Key exists}存储会话数据
Section titled “存储会话数据”可以使用 put() 方法将会话数据存储到会话中。put() 方法接受两个参数:key (一个字符串) 和 value。
// Via Illuminate\Http\Request instance$request->session()->put('key', 'value');
// You can also use the global session helpersession(['another_key' => 'another_value']);删除会话数据
Section titled “删除会话数据”forget() 方法用于从会话中移除指定的项。此方法接受要删除项的 key。
$request->session()->forget('key');要移除会话中的所有数据,使用 flush() 方法:
$request->session()->flush();pull() 方法会从会话中检索一个项,然后在一次操作中将其删除。它接受 key 作为参数并返回该项的值。
$value = $request->session()->pull('key_to_pull', 'default');闪存数据(Flash Data)
Section titled “闪存数据(Flash Data)”有时你可能希望只在下一个请求中将会话项存储起来。你可以使用 flash 方法实现这一点。使用此方法存储的数据仅在随后的 HTTP 请求中可用,然后就会被删除。闪存数据主要用于短期的状态消息:
$request->session()->flash('status', 'Task was successful!');如果你需要让闪存数据在多个请求中持续存在,可以使用 reflash 方法,它会将所有闪存数据保留额外的请求。如果你只需要保留特定的闪存数据,可以使用 keep 方法:
$request->session()->reflash();
$request->session()->keep(['username', 'email']);示例:基本会话用法
Section titled “示例:基本会话用法”步骤 1:通过在终端执行以下 Artisan 命令创建名为 UserSessionController 的控制器:
php artisan make:controller UserSessionController步骤 2:成功执行后,将在 app/Http/Controllers/UserSessionController.php 创建一个新的控制器文件。打开此文件并将其内容替换为以下代码:
app/Http/Controllers/UserSessionController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserSessionController extends Controller{ // Store data in the session public function setSessionData(Request $request) { $request->session()->put('user_name', 'John Doe'); $request->session()->put('user_email', 'john.doe@example.com'); return 'Session data has been set.'; }
// Access data from the session public function getSessionData(Request $request) { if ($request->session()->has('user_name')) { $name = $request->session()->get('user_name'); $email = $request->session()->get('user_email'); return "User Name: " . $name . "<br>Email: " . $email; } else { return 'No data found in the session.'; } }
// Delete data from the session public function removeSessionData(Request $request) { $request->session()->forget('user_name'); // Or remove specific items: // $request->session()->forget(['user_name', 'user_email']); return 'User name has been removed from the session.'; }
// Delete all session data public function flushSessionData(Request $request) { $request->session()->flush(); return 'All session data has been removed.'; }}步骤 3:为这些控制器动作定义路由。打开你的 routes/web.php 文件并添加以下行:
routes/web.php
use App\Http\Controllers\UserSessionController;
Route::get('session/set', [UserSessionController::class, 'setSessionData']);Route::get('session/get', [UserSessionController::class, 'getSessionData']);Route::get('session/remove', [UserSessionController::class, 'removeSessionData']);Route::get('session/flush', [UserSessionController::class, 'flushSessionData']);步骤 4:如果你的 Laravel 开发服务器尚未运行 (php artisan serve),请启动它。
步骤 5:在浏览器中访问以下 URL,将数据存储到会话中:
http://localhost:8000/session/set
浏览器将显示:“Session data has been set.”
步骤 6:访问以下 URL,从会话中检索数据:
http://localhost:8000/session/get
浏览器将显示:“User Name: John Doe
Email: john.doe@example.com”
步骤 7:访问以下 URL,移除特定的会话数据 (user_name):
http://localhost:8000/session/remove
浏览器将显示:“User name has been removed from the session.”
步骤 8:再次访问 http://localhost:8000/session/get。现在它将显示:“User Name:
Email: john.doe@example.com”(或类似内容,因为 user_name 已被移除,但如果未明确移除,email 仍然保留)。如果你移除 user_name 和 user_email,或者清空(flush)了会话,它将显示“No data found in the session.”
步骤 9:要移除所有会话数据,访问:
http://localhost:8000/session/flush
浏览器将显示:“All session data has been removed.”
延伸阅读:有关会话配置和高级用法的更多详情,请参考 Laravel 关于 HTTP 会话的官方文档。