ajur-media/template.flash

Flash messages based on Slim Framework code

0.1.0 2024-02-05 19:01 UTC

This package is not auto-updated.

Last update: 2024-04-30 18:11:43 UTC


README

Flash messages enables you to define transient messages that persist only from the current request to the next request.

Install

Via Composer

$ composer require ajur-media/template.flash

Usage

Slim 3

// Start PHP session
if (!session_id()) @session_start();

App::$flash = new \AJUR\Template\FlashMessages();

// or use DI Container

$app->get('/foo', function ($req, $res, $args) {
    // Set flash message for next request
    App::$flash->addMessage('Test', 'This is a message');

    // Redirect
    return $res->withStatus(302)->withHeader('Location', '/bar');
});

$app->get('/bar', function ($req, $res, $args) {
    // Get flash messages from previous request
    $messages = $this->flash->getMessages();
    print_r($messages);

    // Get the first message from a specific key
    $test = $this->flash->getFirstMessage('Test');
    print_r($test);
});

$app->run();

Please note that a message could be a string, object or array. Please check what your storage can handle.

Testing

$ phpunit

License

The MIT License (MIT). Please see License File for more information.