定制 diecoding/yii2-toastr 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

diecoding/yii2-toastr

Composer 安装命令:

composer require diecoding/yii2-toastr

包简介

Simple flash toastr notifications for Yii2

README 文档

README

Simple flash toastr notifications for Yii2

Latest Stable Version PHP Version Require Total Downloads License

Tests Coverage Status PHPStan Level PHP CS Fixer

Yii2 Toastr uses Toastr library
Demo: https://codeseven.github.io/toastr/demo.html

Table of Contents

Installation

Install via Composer:

composer require diecoding/yii2-toastr "^1.0"

Or add to your composer.json:

{
    "require": {
        "diecoding/yii2-toastr": "^1.0"
    }
}

Dependencies

Usage

Layouts/Views

Add ToastrFlash widget to your layout file (e.g., views/layouts/main.php):

Basic Usage

use diecoding\toastr\ToastrFlash;

ToastrFlash::widget();

Advanced Configuration

use diecoding\toastr\ToastrFlash;

ToastrFlash::widget([
    "typeDefault"       => ToastrFlash::TYPE_INFO,            // (string) default `ToastrFlash::TYPE_INFO`
    "titleDefault"      => "",                                // (string) default `""`
    "messageDefault"    => "",                                // (string) default `""`
    "closeButton"       => false,                             // (bool) default `false`
    "debug"             => false,                             // (bool) default `false`
    "newestOnTop"       => true,                              // (bool) default `true`
    "progressBar"       => true,                              // (bool) default `true`
    "positionClass"     => ToastrFlash::POSITION_TOP_RIGHT,   // (string) default `ToastrFlash::POSITION_TOP_RIGHT`
    "preventDuplicates" => true,                              // (bool) default `true`
    "showDuration"      => 300,                               // (int|null) default `300` in `ms`, `null` for skip
    "hideDuration"      => 1000,                              // (int|null) default `1000` in `ms`, `null` for skip
    "timeOut"           => 5000,                              // (int|null) default `5000` in `ms`, `null` for skip
    "extendedTimeOut"   => 1000,                              // (int|null) default `1000` in `ms`, `null` for skip
    "showEasing"        => "swing",                           // (string) default `swing`, `swing` and `linear` are built into jQuery
    "hideEasing"        => "swing",                           // (string) default `swing`, `swing` and `linear` are built into jQuery
    "showMethod"        => "slideDown",                       // (string) default `slideDown`, `fadeIn`, `slideDown`, and `show` are built into jQuery
    "hideMethod"        => "slideUp",                         // (string) default `slideUp`, `hide`, `fadeOut` and `slideUp` are built into jQuery
    "tapToDismiss"      => true,                              // (bool) default `true`
    "escapeHtml"        => true,                              // (bool) default `true`
    "rtl"               => false,                             // (bool) default `false`
    "skipCoreAssets"    => false,                             // (bool) default `false`, `true` if use custom or external toastr assets
    "options"           => [],                                // (array) default `[]`, Custom Toastr options and override default options
]);

Controllers

Use the standard Yii2 flash message system:

Simple Flash Messages

// Single message
Yii::$app->session->setFlash('error', 'Something went wrong!');
Yii::$app->session->setFlash('success', 'Operation completed successfully!');
Yii::$app->session->setFlash('info', 'Information message');
Yii::$app->session->setFlash('warning', 'Warning message');

Multiple Messages

Yii::$app->session->setFlash('error', [
    'Message 1',
    'Message 2', 
    'Message 3'
]);

Messages with Titles (< v1.4.0)

Yii::$app->session->setFlash('error', [
    ['Title', 'Message']
]);

// Multiple messages with titles
Yii::$app->session->setFlash('error', [
    ['Title 1', 'Message 1'], 
    ['Title 2', 'Message 2'], 
    ['Title 3', 'Message 3']
]);

Advanced Usage with Custom Options (≥ v1.4.0)

// Array format
Yii::$app->session->setFlash('error', [
    ['Title', 'Message', ['timeOut' => 10000]]
]);

// Associative array format
Yii::$app->session->setFlash('error', [
    [
        'title' => 'Custom Title',
        'message' => 'Custom message',
        'options' => [
            'progressBar' => true,
            'timeOut' => 5000,
            'hideDuration' => 1000
        ]
    ]
]);

Complex Example with Multiple Messages

Yii::$app->session->setFlash('info', [
    [
        'title' => 'Step 1 Complete', 
        'message' => 'Data validation passed', 
        'options' => ['progressBar' => true, 'timeOut' => 5000]
    ],
    [
        'title' => 'Step 2 Complete', 
        'message' => 'Data saved successfully',
        'options' => ['progressBar' => false, 'timeOut' => 3000]
    ],
    ['Simple message without custom options']
]);

Testing

Run the test suite using PHPUnit:

# Install dependencies
composer install

# Run tests
composer test

# Run tests with coverage (requires PCOV or Xdebug)
composer test:coverage

# Generate HTML coverage report
composer test:coverage-html

The test suite covers:

  • ToastrBase - Base widget functionality and constants
  • Toastr - Main notification widget properties and methods
  • ToastrFlash - Flash message integration
  • ToastrAsset - Asset bundle configuration

Coverage Requirements:

  • Target: >80% code coverage
  • Supports PHP 7.4, 8.0, 8.1, 8.2, 8.3
  • Automated testing via GitHub Actions

Code Quality

This project maintains high standards through automated quality checks:

  • PHP CodeSniffer - PSR-12 coding standards
  • PHP CS Fixer - Modern PHP code formatting
  • PHPStan - Static analysis (level 5)
# Run all quality checks
composer quality

# Individual commands
composer cs           # Check code style
composer cs:fix       # Auto-fix code style  
composer phpstan      # Run static analysis

Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes
  4. Run quality checks (composer quality)
  5. Run tests (composer test)
  6. Commit your changes (git commit -am 'Add amazing feature')
  7. Push to the branch (git push origin feature/amazing-feature)
  8. Open a Pull Request

Requirements:

  • Write tests for new features
  • Follow PSR-12 coding standards
  • Ensure all quality checks pass
  • Update documentation as needed

License

This package is open-source software licensed under the MIT License.

Author: Sugeng Sulistiyawan
Copyright: © 2018-2025 Sugeng Sulistiyawan

Built with ❤️ for the Yii2 community.

统计信息

  • 总下载量: 30.36k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 14
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 13
  • Watchers: 2
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-08-10

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固