Flashing Messages

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.

Adapters

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

AdapterDescriptionAPI
DirectDirectly outputs the messages passed to the flasherPhalcon\Flash\Direct
SessionTemporarily stores the messages in session, then messages can be printed in the next requestPhalcon\Flash\Session

Usage

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:

  1. <?php
  2. use Phalcon\Flash\Direct as FlashDirect;
  3. use Phalcon\Flash\Session as FlashSession;
  4. // Set up the flash service
  5. $di->set(
  6. 'flash',
  7. function () {
  8. return new FlashDirect();
  9. }
  10. );
  11. // Set up the flash session service
  12. $di->set(
  13. 'flashSession',
  14. function () {
  15. return new FlashSession();
  16. }
  17. );

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

  1. <?php
  2. use Phalcon\Mvc\Controller;
  3. class PostsController extends Controller
  4. {
  5. public function indexAction()
  6. {
  7. }
  8. public function saveAction()
  9. {
  10. $this->flash->success('The post was correctly saved!');
  11. }
  12. }

There are four built-in message types supported:

  1. <?php
  2. $this->flash->error('too bad! the form had errors');
  3. $this->flash->success('yes!, everything went very smoothly');
  4. $this->flash->notice('this a very important information');
  5. $this->flash->warning("best check yo self, you're not looking too good.");

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

  1. <?php
  2. $this->flash->message('debug', "this is debug message, you don't say");

Printing Messages

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

  1. <div class='errorMessage'>too bad! the form had errors</div>
  2. <div class='successMessage'>yes!, everything went very smoothly</div>
  3. <div class='noticeMessage'>this a very important information</div>
  4. <div class='warningMessage'>best check yo self, you're not looking too good.</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:

  1. <?php
  2. use Phalcon\Flash\Direct as FlashDirect;
  3. // Register the flash service with custom CSS classes
  4. $di->set(
  5. 'flash',
  6. function () {
  7. $flash = new FlashDirect(
  8. [
  9. 'error' => 'alert alert-danger',
  10. 'success' => 'alert alert-success',
  11. 'notice' => 'alert alert-info',
  12. 'warning' => 'alert alert-warning',
  13. ]
  14. );
  15. return $flash;
  16. }
  17. );

Then the messages would be printed as follows:

  1. <div class='alert alert-danger'>too bad! the form had errors</div>
  2. <div class='alert alert-success'>yes!, everything went very smoothly</div>
  3. <div class='alert alert-info'>this a very important information</div>
  4. <div class='alert alert-warning'>best check yo self, you're not looking too good.</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:

  1. <?php
  2. use Phalcon\Mvc\Controller;
  3. class ContactController extends Controller
  4. {
  5. public function indexAction()
  6. {
  7. }
  8. public function saveAction()
  9. {
  10. // Store the post
  11. // Using direct flash
  12. $this->flash->success('Your information was stored correctly!');
  13. // Forward to the index action
  14. return $this->dispatcher->forward(
  15. [
  16. 'action' => 'index'
  17. ]
  18. );
  19. }
  20. }

Or using a HTTP redirection:

  1. <?php
  2. use Phalcon\Mvc\Controller;
  3. class ContactController extends Controller
  4. {
  5. public function indexAction()
  6. {
  7. }
  8. public function saveAction()
  9. {
  10. // Store the post
  11. // Using session flash
  12. $this->flashSession->success('Your information was stored correctly!');
  13. // Make a full HTTP redirection
  14. return $this->response->redirect('contact/index');
  15. }
  16. }

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

  1. <!-- app/views/contact/index.phtml -->
  2. <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 session first to successfully use the flashSession messenger.