Created
August 31, 2012 21:15
-
-
Save BaylorRae/3559115 to your computer and use it in GitHub Desktop.
Source code for: http://baylorrae.com/add-flash-messages-to-your-site/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php include 'flash.php'; ?> | |
| ... | |
| <?php foreach( Flash::$messages as $id => $msg ) : ?> | |
| <div class="flash_<?php echo $id ?>"><?php echo $msg ?></div> | |
| <?php endforeach; ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // make sure sessions work on the page | |
| session_start(); | |
| class Flash { | |
| // where all messages are stored | |
| public static $messages = array(); | |
| /* | |
| * A generic function to store flash messages | |
| * | |
| * Flash::add('notice', 'a message to display'); | |
| * | |
| * @param string $name the name/id of the flash | |
| * @param string $message the message to display | |
| */ | |
| public static function add($name, $message) { | |
| $_SESSION['flash_messages'][$name] = $message; | |
| } | |
| /* | |
| * A shortcut to Flash::add() | |
| * | |
| * Flash::notice('a message to display'); | |
| */ | |
| public static function __callStatic($fn, $args) { | |
| call_user_func_array(array('Flash', 'add'), array($fn, $args[0])); | |
| } | |
| } | |
| // if $_SESSION['flash_messages'] isset | |
| // then save them to our class | |
| if( isset($_SESSION['flash_messages']) ) { | |
| self::$messages = $_SESSION['flash_messages']; | |
| } | |
| // reset the session's value | |
| $_SESSION['flash_messages'] = array(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| include 'flash.php'; | |
| // using Flash::add directly | |
| Flash::add('notice', 'Awesome job dude!'); | |
| // using the alias | |
| Flash::alert('lol, you can\'t do that!'); | |
| // calls: Flash::add('alert', 'lol, you can\'t do that!'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment