kriss / yii2-alert-notify
Yii2 Alert Notify
Installs: 37
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 1
Open Issues: 0
Type:yii2-extension
Requires
- yiisoft/yii2: ~2.0.0
This package is auto-updated.
Last update: 2024-12-08 14:02:49 UTC
README
Yii2 Alert Notify
Installation
composer require kriss/yii2-alert-notify -vvv
Usage Ajax Notify
- Create a controller like:
AjaxController
, then add action:
public function actions() { $actions = parent::actions(); $actions['notify'] = [ 'class' => AjaxNotifyAction::class, 'generateInfo' => 'generateNotifyInfo', ]; return $actions; }
- Write
generateNotifyInfo
in controller, this is example:
public function generateNotifyInfo($from) { $info = []; // get info from db or other storage // example if (random_int(0, 999) > 300) { $info[] = [ 'notifyOptions' => [ 'message' => date('H:i:s') . ':Has New Message', 'url' => Url::to(['site/index']), 'target' => '_self', ], 'notifySettings' => [ 'delay' => 0, 'type' => 'info', 'offset' => [ 'x' => 20, 'y' => 70, ], ], 'audioConfig' => [ 'url' => Yii::getAlias('@web/audio/sound1.mp3'), 'count' => 1, 'delay' => 1000, ], ]; } // example End return $info; }
- Use Widget in View, like
layouts/main.php
<?= AjaxNotifyWidget::widget() ?>
- After refresh browser, you will see:
Ajax Notify generateInfo
Result Description
-
notifyOptions: see bootstrap-notify Options
-
notifySettings: see bootstrap-notify Settings
-
audioConfig:
-
url: audio src
-
count: audio play count
-
delay: how long between audio play ended and play next, unit
ms
-
Usage Flush Notify
- Use Widget in View, like
layouts/main.php
<?= FlushNotifyWidget::widget() ?>
- Add flush message in controller or service:
Yii::$app->session->addFlash('success', 'Operate Success'); Yii::$app->session->addFlash('danger', 'Operate danger'); Yii::$app->session->addFlash('error', 'Operate error');
- After refresh browser, you will see: