0
我剛剛在laravel中創建了一個庫類。Laravel庫處理消息
class Message {
public static $data = array();
public function __construct() {
// If session has flash data then set it to the data property
if (Session::has('_messages')) {
self::$data = Session::flash('_messages');
}
}
public static function set($type, $message, $flash = false) {
$data = array();
// Set the message properties to array
$data['type'] = $type;
$data['message'] = $message;
// If the message is a flash message
if ($flash == true) {
Session::flash('_messages', $data);
} else {
self::$data = $data;
}
}
public static function get() {
// If the data property is set
if (count(self::$data)) {
$data = self::$data;
// Get the correct view for the message type
if ($data['type'] == 'success') {
$view = 'success';
} elseif ($data['type'] == 'info') {
$view = 'info';
} elseif ($data['type'] == 'warning') {
$view = 'warning';
} elseif ($data['type'] == 'danger') {
$view = 'danger';
} else {
// Default view
$view = 'info';
}
// Return the view
$content['body'] = $data['message'];
return View::make("alerts.{$view}", $content);
}
}
}
我可以在我的視圖中使用這個類,調用Message :: get()。在控制器中,我可以將消息設置爲Message :: set('info','here message here');它工作得很好。
但是,我不能使用此類用於使用Message :: set('info','success message here。',true)重定向的Flash消息。任何想法,這個代碼有什麼不對?
是的。正常情況下會從會話中檢索閃爍的數據。使用'Session :: get('_ messages');' – lukasgeiter 2015-02-07 13:35:47