你安装在你的控制器动作和轮询它每10秒,如果有更新就会从局部视图返回的通知,如果没有更新不返回任何结果,这是一个给你一个想法的框架实现,注意它不会按原样工作。
在你的布局文件
...
// Your normal layout content
<?php Yii::app()->clientScript->registerScript("poll_ajax_notifications",
'function getNotification(){'.
CHtml::ajax(array(
'url'=>array("//notifications/update"),
'dataType'=>'html',
'type'=>'GET',
'update'=>'#divcontainingNotificationWidget',
)
) . '. }
timer = setTimeout("getNotification()", 10000);
', CClientScript::POS_END);
在您的通知控制器
class NotificationsController extends CController {
....
public function actionUpdate(){
$user = Yii::app()->user->id;
// Your logic logic for finding notifications
if($notificationPresent){ // or any validation to check whether to push data or not
$this->renderPartial('_notificationWidget',array('widgetData'=>$widgetData)); // pass data required by widget here
}
Yii::app()->end();
}
...
}
最后创建视图的局部视图/通知文件夹,名为_notificationsWidget.php
在你看来把你的插件调用
<?php
$this->widget('path.to.my.widget',array(
//widget parameters
));
谢谢Manquer。我绝对可以用这个。 – Shark