Sections

Flash Messages


Overview

Flash messages are used to notify the user about the state of actions he/she made or simply show information to the users. These kinds of messages can be generated using this component.

Adaptadores

This component makes use of adapters to define the behavior of the messages after being passed to the Flasher:

Adaptador Descripción API
Direct Las salidas de los mensajes se transfieren directamente al flasher Phalcon\Flash\Direct
Session Se almacena temporalmente los mensajes en la sesión, luego se pueden imprimir los mensajes en la siguiente consulta Phalcon\Flash\Session

Uso

Usually the Flash Messaging service is requested from the services container. If you’re using Phalcon\Di\FactoryDefault then Phalcon\Flash\Direct is automatically registered as flash service and Phalcon\Flash\Session is automatically registered as flashSession service. You can also manually register it:

<?php

use Phalcon\Flash\Direct as FlashDirect;
use Phalcon\Flash\Session as FlashSession;

// Configurar el servicio flash
$di->set(
    'flash',
    function () {
        return new FlashDirect();
    }
);

// Configurar el servicio flash session
$di->set(
    'flashSession',
    function () {
        return new FlashSession();
    }
);

This way, you can use it in controllers or views:

<?php

use Phalcon\Mvc\Controller;

class PostsController extends Controller
{
    public function indexAction()
    {

    }

    public function saveAction()
    {
        $this->flash->success('La publicación fue correctamente guardada!');
    }
}

There are four built-in message types supported:

<?php

$this->flash->error('Que mal! el formulario tiene errores');

$this->flash->success('Si!, todo esta muy bien');

$this->flash->notice('Esta es una información muy importante');

$this->flash->warning("Mejor que te revisen, no te ves muy bien.");

You can also add messages with your own types using the message() method:

<?php

$this->flash->message('debug', "Este es un mensaje de depuración, no lo digas");

Printing Messages

Messages sent to the flash service are automatically formatted with HTML:

<div class='errorMessage'>Que mal! el formulario tiene errores</div>

<div class='successMessage'>Si!,todo esta muy bien</div>

<div class='noticeMessage'>Esta es una información muy importante</div>

<div class='warningMessage'>Mejor que te revisen, no te ves muy bien.</div>

As you can see, CSS classes are added automatically to the <div>s. These classes allow you to define the graphical presentation of the messages in the browser. The CSS classes can be overridden, for example, if you’re using Twitter Bootstrap, classes can be configured as:

<?php

use Phalcon\Flash\Direct as FlashDirect;

// Registrar el servicio flash con clases CSS personalizadas
$di->set(
    'flash',
    function () {
        $flash = new FlashDirect(
            [
                'error'   => 'alert alert-danger',
                'success' => 'alert alert-success',
                'notice'  => 'alert alert-info',
                'warning' => 'alert alert-warning',
            ]
        );

        return $flash;
    }
);

Then the messages would be printed as follows:

<div class='alert alert-danger'>Que mal! el formulario tiene errores</div>

<div class='alert alert-success'>Si!,todo esta muy bien</div>

<div class='alert alert-info'>Esta es una información muy importante</div>

<div class='alert alert-warning'>Mejor que te revisen, no te ves muy bien.</div>

Implicit Flush vs. Session

Depending on the adapter used to send the messages, it could be producing output directly, or be temporarily storing the messages in session to be shown later. When should you use each? That usually depends on the type of redirection you do after sending the messages. For example, if you make a forward is not necessary to store the messages in session, but if you do a HTTP redirect then, they need to be stored in session:

<?php

use Phalcon\Mvc\Controller;

class ContactController extends Controller
{
    public function indexAction()
    {

    }

    public function saveAction()
    {
        // Store the post

        // Using direct flash
        $this->flash->success('Your information was stored correctly!');

        // Forward to the index action
        return $this->dispatcher->forward(
            [
                'action' => 'index',
            ]
        );
    }
}

Or using a HTTP redirection:

<?php

use Phalcon\Mvc\Controller;

class ContactController extends Controller
{
    public function indexAction()
    {

    }

    public function saveAction()
    {
        // Almacenar una publicación

        // Utilizando flash en sesión
        $this->flashSession->success('Tú información fue almacenada correctamente!');

        // Hacer una redirección HTTP completa
        return $this->response->redirect('contact/index');
    }
}

In this case you need to manually print the messages in the corresponding view:

<!-- app/views/contact/index.phtml -->

<p><?php $this->flashSession->output(); ?></p>

The attribute flashSession is how the flash was previously set into the dependency injection container. You need to start the first to successfully use the `flashSession` messenger.