loveorigami / yii2-notification-wrapper
此模块用于渲染来自会话闪存的消息(具有ajax、pjax支持等)
6.7.2
2016-03-31 00:00 UTC
Requires
- yiisoft/yii2: ~2
README
Yii2-notification-wrapper模块可以渲染来自会话闪存的消息(具有ajax支持)。所有闪存消息都将按setFlash分配的顺序显示。
您可以按照以下方式设置消息
public function actionIndex(){ ... Yii::$app->session->setFlash('error', 'noty error'); Yii::$app->session->setFlash('info', 'noty info'); Yii::$app->session->setFlash('success', 'noty success'); Yii::$app->session->setFlash('warning', 'noty warning'); ... return $this->render('index'); } // or in ajax action public function actionAjax(){ ... Yii::$app->session->setFlash('error', 'ajax error'); Yii::$app->session->setFlash('info', 'ajax info'); Yii::$app->session->setFlash('success', 'ajax success'); Yii::$app->session->setFlash('warning', 'ajax warning'); ... $data = 'Some data to be returned in response to ajax request'; Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; return $data; }
安装
安装此扩展的首选方式是通过composer。
要使用支持层之一的bower包安装,请运行
$ php composer.phar require loveorigami/yii2-notification-wrapper "*" # if you want use it $ php composer.phar require bower-asset/noty "^2.3" $ php composer.phar require bower-asset/jquery-growl "^1.3"
或添加
"loveorigami/yii2-notification-wrapper": "*", "bower-asset/noty": "^2.3", "bower-asset/jquery-growl": "^1.3"
到您的composer.json
文件的require
部分。
配置应用程序
让我们从在配置文件中定义模块开始(@common/config/main.php
)
'modules' => [ 'noty' => [ 'class' => 'lo\modules\noty\Module', ], ],
这就完成了,现在您已安装并配置了模块。
使用方法
此软件包包含一个Wrapper小部件,可以用于定期轮询服务器以获取新通知,并使用Noty(或Toastr、Growl等)等工具在视觉上触发它们。
此小部件应按以下方式在主布局文件中使用
use lo\modules\noty\Wrapper; // for Bootstrap Alert echo Wrapper::widget(); // or for Growl echo Wrapper::widget([ 'layerClass' => 'lo\modules\noty\layers\Growl', ]); // or for Noty echo Wrapper::widget([ 'layerClass' => 'lo\modules\noty\layers\Noty', ]);
高级使用
可以通过小部件中的options
参数自定义每个层。更多信息请参阅文档。
use lo\modules\noty\Wrapper; echo Wrapper::widget([ 'layerClass' => 'lo\modules\noty\layers\Noty', 'layerOptions'=>[ // for every layer (by default) 'layerId' => 'noty-layer', 'customTitleDelimiter' => '|', 'overrideSystemConfirm' => true, 'showTitle' => true, // for custom layer 'registerAnimateCss' => true, 'registerButtonsCss' => true ], // clientOptions 'options' => [ 'dismissQueue' => true, 'layout' => 'topRight', 'timeout' => 3000, 'theme' => 'relax', // and more for this library... ], ]);
某些库可以覆盖链接的系统确认,如下所示
<a href="https://github.com" data-confirm="Are you sure?">Go!</a>
自定义标题
如果您想更改通知标题,可以在我们的消息中使用customTitleDelimiter
echo Wrapper::widget([ 'layerClass' => '...', 'layerOptions' => [ 'customTitleDelimiter' = '|', // by default ], ]);
并将消息设置为操作
public function actionIndex(){ ... Yii::$app->session->setFlash('success', 'CUSTOM TITLE | noty success'); ... }
层ID
如果您想将通知消息粘贴到自定义div中,可以使用layerId
。例如
// In layout: $layerParams = [ 'layerClass' => '...', 'layerOptions' => [ ... ], ]; if (isset($this->params['layerParams']){ $layerParams = \yii\helpers\ArrayHelper::merge($layerParams, $this->params['layerParams']); } echo Wrapper::widget($layerParams); // In view: $this->params['layerParams'] = [ 'layerClass' => 'lo\modules\noty\layers\Alert', 'layerOptions' => [ 'layerId' => 'my-noty-id', ], ]; echo '<div id="my-noty-id"></div>'; // and notification will be placed here
禁用ajax调用后的showNoty
- 在Ajax脚本中
$.ajax({ method: 'GET', dataType: 'json', url: someresouceurl, showNoty: false, // add this for disable notification success: function(data) { } });
- 在Pjax中
Pjax::begin([ 'clientOptions' => [ 'showNoty' => false ] ]); ... pjax container ... Pjax::end();
Noty异常
例如
use lo\modules\noty\exceptions\NotyFlashException; use lo\modules\noty\exceptions\NotyErrorException; public function actionUpdate($id) { $model = $this->findModel($id); try { if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('update', [ 'model' => $model, ]); } } catch (NotyFlashException $e) { $e->catchFlash(); } } protected function findModel($id) { if (($model = Post::findOne($id)) !== null) { return $model; } else { throw new NotyErrorException('The requested page does not exist.'); } }
支持的层
当前支持的层包括
完整安装
添加
"loveorigami/yii2-notification-wrapper": "*", "bower-asset/bootstrap-sweetalert": "^1.0", "bower-asset/igrowl": "*", "bower-asset/jqnotifybar": "^1.5", "bower-asset/jquery-growl": "^1.3", "bower-asset/jquery.notify": "^1.0", "bower-asset/jquery-toast-plugin": "*", "bower-asset/jquery.toaster": "*", "bower-asset/lobibox": "*", "bower-asset/notie": "^3.2", "bower-asset/notifit": "^1.1", "bower-asset/notific8": "^3.5", "bower-asset/notifyjs": "^0.4", "bower-asset/pnotify": "^3.0", "bower-asset/noty": "^2.3", "bower-asset/remarkable-bootstrap-notify": "^3.1", "bower-asset/sweetalert": "^1.1", "bower-asset/toastr": "^2.1"
到您的composer.json
文件的require
部分。
许可证
Yii2-notification-wrapper遵循MIT许可证发布。有关详细信息,请参阅打包的LICENSE.md。