按理说自己实现也不难,不过咱有优雅癖,不想重复造轮子。。。另外主要是懒。。。。
按理说自己实现也不难,不过咱有优雅癖,不想重复造轮子。。。另外主要是懒。。。。
Yii::app()->user->setFlash("message",xxx)
Yii::app()->user->getFlash("message")
获取消息,然后用js弹窗,接着自动关闭。
我用这个
<meta http-equiv="refresh" content="3;url=<?=Yii::app()->homeUrl?>" />
<div class="flash-success">
<?php echo Yii::app()->user->getFlash('newsletterSubmitted'); ?>
</div>
我再来一个进阶版的。
1,在CWebUser扩展类中
public function pushMessage($string=null, $type='successMsg')
{
if ($string!=null ) {
if($this->hasFlash($type))
$old = $this->getFlash($type);
else
$old = array();
array_push($old, $string);
$this->setFlash($type, $old);
}
}
public function showPutMsg()
{
$show ='';
$successName= 'successMsg';
$noticeName= 'noticeMsg';
$errorName= 'errorMsg';
if($this->hasFlash($successName)) {
$show.= '<div class="flash-success">';
$msg= (array) $this->getFlash($successName);
$show.= '<ul>';
for($i=0; $i<count($msg); $i++) {
$show.= '<li>'. $msg[$i]. '</li>';
}
$show.= '</ul></div>';
}
if($this->hasFlash($noticeName)) {
$show.= '<div class="flash-notice">';
$msg= (array) $this->getFlash($noticeName);
$show.= '<ul>';
for($i=0; $i<count($msg); $i++) {
$show.= '<li>'. $msg[$i]. '</li>';
}
$show.= '</div>';
}
if($this->hasFlash($errorName)) {
$show.= '<div class="flash-error">';
$msg= (array) $this->getFlash($errorName);
$show.= '<ul>';
for($i=0; $i<count($msg); $i++) {
$show.= '<li>'. $msg[$i]. '</li>';
}
$show.= '</div>';
}
return $show;
}
public function putSuccessMsg($string=null)
{
$this->pushMessage($string, 'successMsg');
}
public function putNoticeMsg($string=null)
{
$this->pushMessage($string, 'noticeMsg');
}
public function putErrorMsg($string=null)
{
$this->pushMessage($string, 'errorMsg');
}
2,在controller中,可以有三种提示信息
Yii::app()->user->putSuccessMsg('操作成功啦!');
Yii::app()->user->putNoticeMsg('操作警告...');
Yii::app()->user->putErrorMsg('操作失败了!');
3,最后在view中统一弹出便可。
<?php echo Yii::app()->admin->showPutMsg(); ?>
一般不要采用js弹窗形式,用户体验不是很好。