Developer Blog

  • Blog
  • /
  • PHPixie flash messages
By Dracony on 26 February 2013

Flash messagesare a simple way to set a message to be displayed to a user on a different page. For example after successfully editing an article you may want to redirect the user to a listing page, while also notifying him that his changes have been saved. The important part here is that these messages should appear only once, so when the user refreshes the page he is on the message will not be there anymore. Here is how you can now use them in PHPixie:

1
2
3
4
5
//Setting a flash message
Session::flash('success',"You have added a fairy");

//Retrieving the message and assigning it to a view.
$view->success_message=Session::flash('success');

Inside the view you can then do this:

1
2
3
4
5
<?php if($success_message): ?>
    <div id="message">
        <?php echo $success_message; ?>
    </div>
<?php endif; ?>

Or you may check the session directly inside the view, skipping passing the variable from controller:

1
2
3
4
5
<?php if($message = Session::flash('success')): ?>
    <div id="message">
        <?php echo $message; ?>
    </div>
<?php endif; ?>
comments powered by Disqus