kriss/yii2-alert-notify

Yii2 Alert Notify

Installs: 32

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 1

Open Issues: 0

Type:yii2-extension

v1.1 2019-11-08 02:52 UTC

This package is auto-updated.

Last update: 2024-04-08 12:22:11 UTC


README

Yii2 Alert Notify

Installation

composer require kriss/yii2-alert-notify -vvv

Usage Ajax Notify

  1. Create a controller like: AjaxController, then add action:
public function actions()
{
    $actions = parent::actions();

    $actions['notify'] = [
        'class' => AjaxNotifyAction::class,
        'generateInfo' => 'generateNotifyInfo',
    ];

    return $actions;
}
  1. 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;
}
  1. Use Widget in View, like layouts/main.php
<?= AjaxNotifyWidget::widget() ?>
  1. After refresh browser, you will see:

preview1

Ajax Notify generateInfo Result Description

Usage Flush Notify

  1. Use Widget in View, like layouts/main.php
<?= FlushNotifyWidget::widget() ?>
  1. 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');
  1. After refresh browser, you will see:

preview1