Skip to content

Instantly share code, notes, and snippets.

@robotamer
Created June 27, 2012 23:16
Show Gist options
  • Save robotamer/3007511 to your computer and use it in GitHub Desktop.
Save robotamer/3007511 to your computer and use it in GitHub Desktop.
A messanger for the Laravel Framework
<?php
class Msg {
public static $msgss = array();
/**
* Add a message to the message array (adds to the user's session)
* @param string $type You can have several types of messages, these are class names for Bootstrap's messaging classes, usually, info, error, success, warning
* @param string $message The message you want to add to the list
*/
public static function add($type = 'info', $message = FALSE){
if(!$message) return FALSE;
if(is_array($message)){
foreach($message as $msg){
static::$msgss[$type][] = $msg;
}
}else{
static::$msgss[$type][] = $message;
}
Session::flash('messages', static::$msgss);
}
/**
* Pull back those messages from the session
* @return array
*/
public static function get(){
return (Session::has('messages')) ? Session::get('messages') : FALSE;
}
/**
* Gets all the messages from the session and formats them accordingly for Twitter bootstrap.
* @return string
*/
public static function getHtml(){
$output = FALSE;
if (Session::has('messages')){
$messages = Session::get('messages');
foreach($messages as $type => $msgs){
if(is_integer($type)) $type = 'error';
$output .= '<div class="alert alert-'.$type.'"><a class="close" data-dismiss="alert">×</a>';
if(is_array($msgs)){
foreach($msgs as $msg) $output .= '<p>'.$msg.'</p>';
}else{
$output .= '<p>'.$msgs.'</p>';
}
$output .= '</div>';
}
}
return $output;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment