phico/session

Phico 的专用会话支持

v0.1.0 2024-07-05 11:12 UTC

This package is auto-updated.

Last update: 2024-09-19 16:29:47 UTC


README

Phico 的专用会话支持 Phico

安装

使用 composer 安装

composer require phico/session

配置

会话配置应该以数组的形式传递

$config = [

    // set the cookie parameters
    'cookie' => [

        'name' => 'ssn',

        'options' => [
            'expires' => 0,
            'path' => '/',
            'domain' => '',
            'secure' => false,
            'httponly' => true,
            'samesite' => 'Lax',
            'prefix' => '',
            'encode' => false,
        ],

    ],

    // set the Time To Live in seconds, sessions will expire after this time
    'ttl' => 3600
];

使用方法

实例化会话

使用会话中间件将会话通过请求传递。

public function use(): array
{
    return [
        \Phico\Session\SessionMiddleware::class
    ]
}

存储数据

要在会话中存储数据,请使用 set 方法。

$session->set('key', 'value');

// or shorthand
$session->key = $value;

检索数据

要从会话中检索数据,请使用 get 方法。您可以为不存在的键提供一个默认值,该值将被返回。

$value = $session->get('key');

// optionally specify a default value if key is not in the session
$value = $session->get('key', 'default');

// or shorthand without specifying a default
$value = $session->key;

检查数据

要检查会话中是否存在键,请使用 has 方法。

$exists = $session->has('key');

闪存消息

闪存消息用于存储仅对下一个请求可用的数据。

设置闪存消息

$session->flash('flash_key', 'flash_value');

检索闪存消息

使用相同的 get 方法检索闪存消息。

$flashValue = $session->get('flash_key');

删除会话

要删除会话,请使用 delete 方法。这将从存储中删除会话数据。

$session->delete();

访问会话 ID

会话 ID 是只读的

$id = $session->id;

重新生成会话 ID

要创建新的会话 ID,请使用 regenerate 方法,这将删除存储中的旧会话。

$session->regenerate();

问题

如果您发现任何关于行为或性能的任何错误或问题,请创建一个问题,如果可能的话,请提交一个带有修复的拉取请求。

请确保根据需要更新任何测试。

对于重大更改,请首先打开一个问题以讨论您想要更改的内容。

许可

BSD-3-Clause