2017 © Pedro Peláez
 

library paddy

Session based (flash) messaging utility.

image

gajus/paddy

Session based (flash) messaging utility.

  • Monday, July 6, 2015
  • by gajus
  • Repository
  • 2 Watchers
  • 2 Stars
  • 17 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

Paddy

Build Status Coverage Status Latest Stable Version, (*1)

Messenger is used to carry (flash) messages between page requests using session., (*2)

/**
 * @param string $namespace Namespace is used if more than one application is using Messenger. Defaults to the SERVER_NAME or "default".
 */
$messenger = new \Gajus\Paddy\Messenger();

/**
 * @param string $message
 * @param string $namespace Message namespace (success, error or notice).
 * @return $this
 */
$messenger->send('Loaded to the Gunwales!');

Sending a Message

Message is sent using send method. The second parameter is used to put message under a namespace., (*3)

$messenger->send('There is more grog on the deck!', 'success');

Namespace values are limited to "error", "notice" and "success". Limit is imposed to avoid accidental (and hard to catch) typos. If you would like to change this behaviour, raise an issue including an example use case for an alternative or a variable namespace., (*4)

Getting Messages

To get all messages nested under the respective namespace, use getMessages method:, (*5)

/**
 * Return all messages nested under the respective message namespace.
 * 
 * @return array
 */
$messenger->getMessages();

To check if there are messages under a specific namespace, use has method:, (*6)

/**
 * Check if there are messages under the specified message namespace.
 * 
 * @param string $namespace
 * @return boolean
 */
$messenger->has('error');

Message Holder

Use the message holder when you intend to display messages to the end user:, (*7)

$messenger->send('a');
$messenger->send('b', 'success');

echo $messenger->getMessageHolder();
<ul class="paddy-messenger with-messages">
    <li class="error">a</li>
    <li class="success">b</li>
</ul>

When there are no messages, getMessageHolder will produce:, (*8)

<ul class="paddy-messenger no-messages"></ul>

The empty tag is used for interoperability with the frontend script., (*9)

Proposed stylesheet:, (*10)

.paddy-messenger {
    display: none;

    li {
        display: block; padding: 20px; color: #fff;

        &.error {
            background: #E74C3C;
        }

        &.notice {
            background: #F1C40F;
        }

        &.important {
            background: #3498DB;
        }

        &.success {
            background: #27AE60;
        }
    }

    &.with-messages {
        display: block;
    }
}

Shorthand

/**
 * Shorthand method to send message under "error" namespace.
 *
 * @param string $message
 * @return $this
 */
public function error ($message) {
    return $this->send($message, 'error');
}

/**
 * Shorthand method to send message under "success" namespace.
 *
 * @param string $message
 * @return $this
 */
public function success ($message) {
    return $this->send($message, 'success');
}

/**
 * Shorthand method to send message under "notice" namespace.
 *
 * @param string $message
 * @return $this
 */
public function notice ($message) {
    return $this->notice($message, 'notice');
}

Quasi-Persistency

Messages are carried across pages using $_SESSION variable. This requires that you start session before using Paddy., (*11)

// Page 1
$messenger->error('foo');

Response that has "location" header continue to persist message data:, (*12)

// Page 2
header('Location: Page 3');

Messages are discarded after the first page is displayed:, (*13)

// Page 3
var_dump($messenger->has('error'));
array(1) {
  [0]=>
  bool(true)
}

Name

Named after pigeon number NPS.43.9451, Paddy., (*14)

Logging

Implements PSR-3 LoggerAwareInterface., (*15)

The Versions

06/07 2015

dev-master

9999999-dev https://github.com/gajus/paddy

Session based (flash) messaging utility.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4

 

The Development Requires

by Gajus Kuizinas

message flash

04/12 2014

0.1.3

0.1.3.0 https://github.com/gajus/paddy

Session based (flash) messaging utility.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4

 

The Development Requires

by Gajus Kuizinas

message flash

04/12 2014

0.1.2

0.1.2.0 https://github.com/gajus/paddy

Session based (flash) messaging utility.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4

 

The Development Requires

by Gajus Kuizinas

message flash

16/04 2014

0.1.1

0.1.1.0 https://github.com/gajus/paddy

Session based (flash) messaging utility.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4

 

The Development Requires

by Gajus Kuizinas

message flash

16/04 2014

0.1.0

0.1.0.0 https://github.com/gajus/paddy

Session based (flash) messaging utility.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4

 

The Development Requires

by Gajus Kuizinas

message flash