joefallon/phpflash

A simple library for flash messages.

v3.0.1 2016-09-18 21:31 UTC

This package is auto-updated.

Last update: 2024-04-25 22:16:39 UTC


README

By Joe Fallon

The flash is a special part of the session which is cleared with each request. This means that values stored there will only be available in the next request. This is useful for passing error/success/warning/info messages to the user.

This library has the following features:

  • Fully unit tested.
  • Allows flash message to be stored in either the session or in a temporary variable.
  • They following types of flash messages can be stored: info, warning, success, and error.

Installation

The easiest way to install PHP Flash is with Composer. Create the following composer.json file and run the php composer.phar install command to install it.

{
    "require": {
        "joefallon/phpflash": "*"
    }
}

Usage

Types of Messages

info    - Some event occurred that the user should be aware of.
warning - Something not good happened, but it isn't an error.
success - Whatever was attempted did, in fact, succeed.
error   - Some sort of program error occurred.

Working with Messages in the Session

To load and clear the messages that are in the session call the following:

loadMessagesFromSession()

Calling #loadMessagesFromSession is required because once PHP begins sending the page data back to the client, the session can no longer be accessed.

Info Messages

storeInfoMessage($message, $storeInSession = true)
retrieveInfoMessages()

Success Messages

storeSuccessMessage($message, $storeInSession = true)
retrieveSuccessMessages()

Warning Messages

storeWarningMessage($message, $storeInSession = true)
retrieveWarningMessages()

Error Messages

storeErrorMessage($message, $storeInSession = true)
retrieveErrorMessages()

Example Usage on Two Separate Page Requests

$flash1 = new FlashMessages();
$flash1->storeInfoMessage('my info message', true);

later on the next request...

$flash2 = new FlashMessages();
$flash2->loadMessagesFromSession();
$infoMessages = $flash2->retrieveInfoMessages();

Example Usage on the Same Page Request

$flash = new FlashMessages();
$flash->storeInfoMessage('my info message', false);

later on in the same request...

$infoMessages = $flash->retrieveInfoMessages();