diecoding / yii2-toastr
Simple flash toastr notifications for Yii2
Fund package maintenance!
sugeng-sulistiyawan
Installs: 24 098
Dependents: 1
Suggesters: 0
Security: 0
Stars: 12
Watchers: 2
Forks: 2
Open Issues: 0
Requires
- php: >=7.4.0
- npm-asset/toastr: ~2.1.0
- yiisoft/yii2: ~2.0.0
Requires (Dev)
- pestphp/pest: ^1.22
README
Simple flash toastr notifications for Yii2
Yii2 Toastr uses Toastr
Demo: https://codeseven.github.io/toastr/demo.html
Table of Contents
Installation
Package is available on Packagist, you can install it using Composer.
composer require diecoding/yii2-toastr "^1.0"
or add to the require section of your composer.json
file.
"diecoding/yii2-toastr": "^1.0"
Dependencies
- PHP 7.4+
- yiisoft/yii2
- npm-asset/toastr
Usage
Layouts/Views
Add
ToastrFlash
to your layout or view file, example in fileviews\layouts\main.php
Layouts/Views Simple Usage
use diecoding\toastr\ToastrFlash; ToastrFlash::widget();
Layouts/Views Advanced Usage
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
Just use
Yii::$app->session->setFlash($type, $message)
like as usual alert
Controllers Simple Usage
Yii::$app->session->setFlash('error', 'Message');
or if use multiple flash in same session
Yii::$app->session->setFlash('error', [(string) 'Message 1', (string) 'Message 2', (string) 'Message 3']);
Controllers Advanced Usage (< v1.4.0)
Yii::$app->session->setFlash('error', [[(string) 'Title', (string) 'Message']]);
or if use multiple flash in same session
Yii::$app->session->setFlash('error', [['Title 1', 'Message 1'], ['Title 2', 'Message 2'], ['Title 3', 'Message 3']]);
Controllers Advanced Usage With Override Toastr Options (≥ v1.4.0)
Yii::$app->session->setFlash('error', [[(string) 'Title', (string) 'Message', (array) 'Options']]); // or Yii::$app->session->setFlash('error', [['title' => (string) 'Title', 'message' => (string) 'Message', 'options' => (array) 'Options']]);
or if use multiple flash in same session
Yii::$app->session->setFlash('error', [ [ 'Title 1', 'Message 1', [ "progressBar" => true, "showDuration" => 300, "hideDuration" => 10000, "timeOut" => 5000, "extendedTimeOut" => 1000, ] ], [ 'title' => 'Title 2', 'message' => 'Message 2', 'options' => [ "progressBar" => false, "hideDuration" => 10000, ] ], ['Title 3', 'Message 3'], ['Message 4'], [ 'message' => 'Message 5', 'options' => [ "progressBar" => false, ] ], [ 'title' => 'Title 6', 'options' => [ "timeOut" => 50000, ] ], ]);
Testing
This package uses Pest for testing. To run the tests:
# Install dependencies composer install # Run tests composer test # Run tests with verbose output composer test:verbose # Run all tests (including experimental) composer test:all # Using the test script ./run-tests.sh
Test Coverage
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
All tests are automatically run on multiple PHP versions (7.4, 8.0, 8.1, 8.2, 8.3) via GitHub Actions.
Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Make your changes
- Run tests to ensure everything works (
composer test
) - Commit your changes (
git commit -am 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
Please make sure to:
- Write tests for new features
- Follow the existing code style
- Update documentation as needed