neoflow/session

此包已被废弃且不再维护。作者建议使用 odan/session 包。

为 Slim 4 和类似 PSR-15 兼容框架和应用程序提供的会话服务。

2.1.0 2021-12-16 09:24 UTC

This package is auto-updated.

Last update: 2023-06-12 09:21:41 UTC


README

请使用其他解决方案来处理 PHP 中的会话。

会话

Build Status Coverage Status Latest Stable Version Total Downloads License

为 Slim 4 和类似 PSR-15 兼容框架和应用程序提供的会话服务。

目录

需求

  • PHP >= 7.3

安装

您有2种方式来安装这个库。

通过 composer...

composer require neoflow/session

...或从这里手动下载最新版本。

配置

以下说明基于 Slim 4,并结合 PHP-DI,但应适用于任何 PSR-11/PSR-15 兼容框架和库。

将服务 Neoflow\Session\Session 和中间件 Neoflow\Session\Middleware\SessionMiddleware 添加到容器定义中...

use Neoflow\Session\Session;
use Neoflow\Session\SessionInterface;
use Neoflow\Session\Middleware\SessionMiddleware;
use Psr\Container\ContainerInterface;

return [
    // ...
    SessionInterface::class => function () {
        return new Session([ // Default session options
            'name' => 'sid',
            'autoRefresh' => true,
            'cookie' => [
                'lifetime' => 3600,
                'path' => '/',
                'domain' => null,
                'secure' => false,
                'httponly' => true,
                'samesite' => 'Lax'
            ],
            'iniSettings' => []
        ]);
    },
    SessionMiddleware::class => function (ContainerInterface $container) {
        $session = $container->get(SessionInterface::class);
        return new SessionMiddleware($session);
    },
    // ...
];

...并注册中间件,以便在分发时自动启动会话。

use Neoflow\Session\Middleware\SessionMiddleware;

$app->add(SessionMiddleware::class);

服务 Neoflow\Session\Session 支持以下选项

类型 描述 默认值
name 字符串 会话cookie的名称。 "sid"
autoRefresh 布尔值 每次请求后刷新会话生命周期。 true
cookie['lifetime'] 整数 会话cookie在秒中的生命周期 3600
cookie['path'] 字符串 设置会话cookie的路径 "/"
cookie['domain'] 字符串/null 设置会话cookie的域 null
cookie['secure'] 布尔值 设置为仅通过安全连接发送会话cookie false
cookie['httponly'] 布尔值 设置为使会话cookie可用于脚本语言 true
cookie['samesite'] 字符串 设置为防止会话cookie与跨站请求一起发送 "Lax"
iniSettings[] 数组 PHP会话设置,不包括 session. []

当您的DI容器支持inflectors(例如league/container)时,您可以选择将 Neoflow/Session/SessionAwareInterface 注册为inflector到您的容器定义。

此外,您还可以使用 Neoflow/Session/SessionAwareTrait 作为 Neoflow/Session/SessionAwareInterface 的简写实现。

使用

处理会话的示例

// Set session name.
$name = 'sid'; // Session name
$session = $session->setName($name);

// Set session cookie.
$session = $session->setCookie([
    // Cookie options
]);

// Start session.
$started = $session->start();

// Get session status.
$status = $session->getStatus();

// Check whether session is started.
$isStarted = $session->isStarted();

// Generate new session id.
$id = $session->generateId();

// Get session cookie.
$cookie = $session->getCookie();

// Get session id.
$id = $session->getId();

// Get session name.
$name = $session->getName();

// Destroy session.
$destroyed = $session->destroy();

如何访问和管理会话值示例

// Get session value by key.
$default = null; // Default value, when key doesn't exists
$value = $session->getValue('key', $default);

// Set session value by key.
$overwrite = true; // Set FALSE to prevent overwrite existing value
$session = $session->setValue('key', 'value', $overwrite);

// Check whether session value exists by key.
$valueExists = $session->hasValue('key');
   
// Delete session value by key.
$session->deleteValue('key');

// Count number of session values.
$numberOfValues = $session->countValues();

// Get session values.
$values = $session->getValues();

// Clear session values.
$session = $session->clearValues();

// Replace session values by key. Existing values with similar keys will be overwritten.
$recursive = true; // Set TRUE to enable recursive replacement
$session = $session->replaceValues([
    // Array with key/value pairs
], $recursive);

// Set session values. Existing values will be overwritten.
$session = $session->setValues([
    // Array with key/value pairs
]);

闪存消息

这个库的第一个版本内置了对闪存消息的支持。但为了符合关注点分离的设计原则,闪存消息的代码被移动到一个独立的库中,称为Neoflow\FlashMessages

如果您需要闪存消息的支持,可以轻松地将这两个库作为composer包结合使用。《Neoflow\FlashMessages》的集成和使用与当前库非常相似。

贡献者

如果您想看到这个库进一步发展,或者如果您想支持我或表达您的感谢,请通过PayPal捐赠任何金额。谢谢!🍻

Donate

许可

许可协议为MIT

瑞士制造,配有🧀和❤️