gbksoft / yii2-toastr
This is the Toastr extension for Yii 2. It encapsulates Toastr plugin in terms of Yii widgets, and makes ajax notification easy to implement.
Installs: 1 590
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 4
Forks: 11
Type:yii2-extension
Requires
- bower-asset/toastr: *
- yiisoft/yii2: *
This package is not auto-updated.
Last update: 2024-12-21 20:30:28 UTC
README
This is the Toastr extension for Yii 2. It encapsulates Toastr plugin in terms of Yii widgets, and makes ajax notification easy to implement.
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist gbksoft/yii2-toastr "*"
or add
"gbksoft/yii2-toastr": "*"
to the require section of your composer.json
file.
Usage
Once the extension is installed, you can test that the extension works by simply use it in your code by :
<?= \gbksoft\yii2toastr\Toastr::widget([ 'toastType' => 'error', 'message' => 'This is an error.', 'customStyle' => false ]);?>
There are 2 main useful widgets
ToastrFlash
displays Yii flash messages in toastr notification style
<?php $session = \Yii::$app->getSession(); $session->setFlash('error', "msg1"); $session->setFlash('danger', "msg2"); $session->setFlash('warning', "msg3"); $session->setFlash('info', "msg4"); $session->setFlash('success', "msg5"); ?>
<?= \gbksoft\yii2toastr\ToastrFlash::widget([ 'options' => [ 'positionClass' => 'toast-bottom-left' ] ]);?>
ToastrAjaxFeed
fetch notification from ajax url
<?= \gbksoft\yii2toastr\ToastrAjaxFeed::widget([ 'feedUrl' => yii\helpers\Url::toRoute('/user/profile/notification-feed'), 'interval' => 5000, 'options' => [ 'positionClass' => 'toast-bottom-left' ] ]);?>
the ajax controller should return an array like this
public function actionNotificationFeed(){ $ret = [ [ 'type' => 'error', 'message' => 'error message', 'title' => 'Hey!' ], [ 'type' => 'info', 'message' => 'another message', 'title' => 'Hello' ] ]; return \yii\helpers\Json::encode($ret); }