Flash Messages


Related Links

Introduction

Flash messages are session based messages that display on the next page load and are then removed. This article will describe how to display flash messages to users.

Basic Usage

To get started you will need to inject the \Anomaly\Streams\Platform\Message\MessageBag into your class.

<?php namespace App\Controller;

use Anomaly\Streams\Platform\Message\MessageBag;

public function hello(MessageBag $messages) {

    $messages->success('Why hello there!');
    $messages->error('Now, go away.');

    return $this->view->make('theme::hi');
}

Displaying Messages

A basic Twig example that's included in the make:theme command for you looks like this:

{% verbatim %}
<!-- Success Messages -->
{% if message_exists('success') %}
    <div class="alert alert-success">
        <button type="button" class="close" data-dismiss="alert">
            <span>&times;</span>
        </button>
        {% for message in message_get('success') %}
            {{ trans(message)|markdown }}
        {% endfor %}
    </div>
{% endif %}

<!-- Informational Messages -->
{% if message_exists('info') %}
    <div class="alert alert-info">
        <button type="button" class="close" data-dismiss="alert">
            <span>&times;</span>
        </button>
        {% for message in message_get('info') %}
            {{ trans(message)|markdown }}
        {% endfor %}
    </div>
{% endif %}

<!-- Warning Messages -->
{% if message_exists('warning') %}
    <div class="alert alert-warning">
        <button type="button" class="close" data-dismiss="alert">
            <span>&times;</span>
        </button>
        {% for message in message_get('warning') %}
            {{ trans(message)|markdown }}
        {% endfor %}
    </div>
{% endif %}

<!-- Error Messages -->
{% if message_exists('error') %}
    <div class="alert alert-danger">
        <button type="button" class="close" data-dismiss="alert">
            <span>&times;</span>
        </button>
        {% for message in message_get('error') %}
            {{ trans(message)|markdown }}
        {% endfor %}
    </div>
{% endif %}
{% endverbatim %}

Note that in this example the messages are parsed by the markdown filter as well.