neoflow/flash

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

适用于Slim 4及类似PSR-15兼容框架和应用程序的Flash服务。

2.0.1 2022-12-23 12:50 UTC

This package is auto-updated.

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


README

请使用其他方案在PHP中处理Flash消息。

Flash

Build Status Latest Stable Version Total Downloads License

适用于Slim 4及类似PSR-15兼容框架和应用程序的Flash服务。

目录

需求

  • PHP >= 7.3

安装

您有两种方式安装此库。

通过Composer...

composer require neoflow/flash

...或者从此处手动下载最新版本。

配置

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

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

use Neoflow\Flash\Flash;
use Neoflow\Flash\FlashInterface;
use Neoflow\Flash\Middleware\FlashMiddleware;
use Psr\Container\ContainerInterface;

return [
    // ...
    FlashInterface::class => function () {
        $key = '_flash'; // Key as identifier of the values in the storage
        return new Flash($key);
    },
    FlashMiddleware::class => function (ContainerInterface $container) {
        $flash = $container->get(FlashInterface::class);
        return new FlashMiddleware($flash);
    },
    // ...
];

...并注册中间件,以会话作为值的存储。

use Neoflow\Flash\Middleware\FlashMiddleware;

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

请注意会话必须先开始,然后中间件才能成功分发。

或者,您也可以使用闭包中间件从会话以外的存储加载值...

$app->add(function ($request, $handler) use ($container) {
    $storage = [ 
        // Your custom storage of the values
    ];
    $container->get(FlashInterface::class)->load($storage);
    return $handler->handle($request);
});

...或者直接在容器定义中添加它并跳过中间件。

use Neoflow\Flash\Flash;
use Neoflow\Flash\FlashInterface;

return [
    // ...
    FlashInterface::class => function () {
        $key = '_flash'; // Key as identifier of the values in the storage
        $storage = [
            // Your custom storage of the values
        ];
        return new Flash($key, $storage);
    },
    // ...
];

当您的DI容器支持变形器(例如league/container)时,您可以选择将Neoflow/Flash/FlashAwareInterface注册为变形器到容器定义中。

此外,您也可以使用Neoflow/Flash/FlashAwareTrait作为Neoflow/Flash/FlashAwareInterface的简写实现。

使用

Neoflow\Flash\Flash服务提供了访问当前请求的值和为下一个请求添加值所需的最基本方法。

// Set a value by key for the next request.
$key = 'key'; // Key as identifier of the value
$flash = $flash->set($key, 'Your custom value');

// Get value by key, set for current request.
$default = null; // Default value, when value doesn't exists or is empty (default: null)
$value = $flash->get($key, $default);

// Check whether value by key for current request exists.
$exists = $flash->has($key);

// Count number of values for current request.
$numberOfValues = $flash->count();

// Clear values of current and next request.
$flash = $flash->clear();

// Keep current values for next request. Existing values will be overwritten.
$flash = $flash->keep(); 

// Load values from storage as reference.
$storage = [
    '_flash' => []
];
$flash = $flash->load($storage);

对于更复杂的使用案例,您也可以获取和设置当前和下一个请求的值。

// Get values set for next request.
$nextValues = $flash->getNext();

// Set values for next request. Existing values will be overwritten.
$flash = $flash->setNext([
    'key1' => 'value1'
]);

// Get values set for current request.
$currentValues = $flash->getCurrent();

// Set values for current request. Existing values will be overwritten.
$flash = $flash->setCurrent([
    'key1' => 'value1'
]);

值得了解的

版本2.0已简化,并且已删除消息处理实现。如果您需要消息处理,请继续使用版本1.2。

在过去,该库也是Neoflow\Session的一部分,后来将其移动到独立的库中,以符合关注点分离的设计原则。

如果您想使用会话服务,可以轻松地将这两个库作为composer包结合使用。Neoflow\Session的集成和使用与当前库非常相似。

贡献者

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

Donate

许可

MIT许可下。

瑞士制造,用🧀和❤️