maxodrom/yii2-toastr

这是为 Yii 2 定制的 Toastr 扩展。它将 Toastr 插件封装成 Yii 小部件,使得 AJAX 通知的实现变得简单。

安装: 60

依赖者: 0

建议者: 0

安全性: 0

星标: 0

关注者: 2

分支: 11

类型:yii2-extension

1.1.1 2018-06-30 21:31 UTC

This package is not auto-updated.

Last update: 2024-09-29 06:02:19 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
]);?>

有两个主要的有用小部件:

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);
}