Components · v1.x

Flash

The Flash component stores messages in the session until they are read, to display them after a redirect. It requires the Session component.

Adding messages

In a controller (trait FlashController):

$this->addFlash('success', 'Your profile has been saved.');

In a service, inject NeoPHP\Component\Flash\Contract\FlashInterface:

$this->flash->add('error', 'The payment failed.');

Displaying messages

The view helper flashes($type = null) reads and removes the messages: with a type, it returns a list of messages; without, an array [type => messages].

{% for message in flashes('success') %}
    <div class="alert alert-success">{{ message }}</div>
{% endfor %}

{% for type, messages in flashes() %}
    {% for message in messages %}
        <div class="alert alert-{{ type }}">{{ message }}</div>
    {% endfor %}
{% endfor %}
<?php foreach ($this->flashes('success') as $message): ?>
    <div class="alert alert-success"><?= $this->e($message) ?></div>
<?php endforeach ?>

FlashInterface

Implemented by FlashManager (extending Contract\AbstractFlash).

Method Description
add($type, $message) adds a message
get($type) reads and removes the messages of a type
all() reads and removes all the messages
peek($type), peekAll() reads without removing
has($type) whether a type has messages
clear() removes all the messages

Configuration

config/framework/app.yaml:

flash:
  key: _flashes

key is the session key where the messages are stored.

Changelog

  • v1.6.0 — Flash component: FlashInterface, addFlash(), flashes() view helper.