odaialali / yii2-toastr
这是为 Yii 2 开发的 Toastr 扩展。它将 Toastr 插件封装成 Yii 小部件,使得 AJAX 通知的实现变得简单。
1.0b
2015-01-28 05:23 UTC
Requires
- bower-asset/toastr: *
- yiisoft/yii2: *
This package is not auto-updated.
Last update: 2024-09-14 17:11:05 UTC
README
这是为 Yii 2 开发的 Toastr 扩展。它将 Toastr 插件封装成 Yii 小部件,使得 AJAX 通知的实现变得简单。
安装
安装此扩展的首选方式是通过 composer。
运行以下命令之一:
php composer.phar require --prefer-dist odaialali/yii2-toastr "*"
或者
"odaialali/yii2-toastr": "*"
将以下内容添加到您的 composer.json
文件的 require 部分中。
用法
一旦扩展安装完成,您可以通过在代码中使用它来测试扩展是否正常工作
<?= \odaialali\yii2toastr\Toastr::widget([ 'toastType' => 'error', 'message' => 'This is an error.', 'customStyle' => false ]);?>
有 2 个主要的实用小部件
ToastrFlash
以 toastr 通知样式显示 Yii 的闪存消息
<?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"); ?>
<?= \odaialali\yii2toastr\ToastrFlash::widget([ 'options' => [ 'positionClass' => 'toast-bottom-left' ] ]);?>
ToastrAjaxFeed
从 AJAX URL 获取通知
<?= \odaialali\yii2toastr\ToastrAjaxFeed::widget([ 'feedUrl' => yii\helpers\Url::toRoute('/user/profile/notification-feed'), 'interval' => 5000, 'options' => [ 'positionClass' => 'toast-bottom-left' ] ]);?>
AJAX 控制器应返回类似以下的数组
public function actionNotificationFeed(){ $ret = [ [ 'type' => 'error', 'message' => 'error message', 'title' => 'Hey!' ], [ 'type' => 'info', 'message' => 'another message', 'title' => 'Hello' ] ]; return \yii\helpers\Json::encode($ret); }