neoflow/flash

This package is abandoned and no longer maintained. The author suggests using the odan/session package instead.

Flash service for Slim 4 and similar PSR-15 compliant frameworks and apps.

2.0.1 2022-12-23 12:50 UTC

This package is auto-updated.

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


README

Please use other solutions for flash messages with PHP.

Flash

Build Status Latest Stable Version Total Downloads License

Flash service for Slim 4 and similar PSR-15 compliant frameworks and apps.

Table of Contents

Requirement

  • PHP >= 7.3

Installation

You have 2 options to install this library.

Via Composer...

composer require neoflow/flash

...or manually download the latest release from here.

Configuration

The following instructions based on Slim 4, in combination with PHP-DI, but should be adaptable for any PSR-11/PSR-15 compliant frameworks and libraries.

Add the service Neoflow\Flash\Flash and middleware Neoflow\Flash\Middleware\FlashMiddleware to the container definitions...

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);
    },
    // ...
];

...and register the middleware, to use the session as storage for the values.

use Neoflow\Flash\Middleware\FlashMiddleware;

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

Please note The session has to start first, before the middleware can get successfully dispatched.

Alternatively, you can also load values from another storage than the session with a closure middleware...

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

...or add it directly in the container definitions and skip the middleware.

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);
    },
    // ...
];

When your DI container supports inflectors (e.g. league/container), you can optionally register Neoflow/Flash/FlashAwareInterface as inflector to your container definition.

Additionally, you can also use Neoflow/Flash/FlashAwareTrait as a shorthand implementation of Neoflow/Flash/FlashAwareInterface.

Usage

The service Neoflow\Flash\Flash provides the most needed methods to get access to the values for the current request and to add values for the next request.

// 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);

For more advanced use cases, you can also get and set the values for current and next request.

// 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'
]);

Nice to know

Version 2.0 has been simplified and the message handling implementation has been removed. If you need the message handling please keep using version 1.2.

In earlier times, the library was also part of Neoflow\Session and then moved into a standalone library, to comply with the design principle of separation of concerns.

If you want to use a session service, you can easily combine both libraries as composer packages. The integration and usage of Neoflow\Session is very similar to the current library.

Contributors

If you would like to see this library develop further, or if you want to support me or show me your appreciation, please donate any amount through PayPal. Thank you! 🍻

Donate

License

Licensed under MIT.

Made in Switzerland with 🧀 and ❤️