Laravel 中的 Flash 通知

3.1.0 2023-05-19 07:08 UTC

This package is auto-updated.

Last update: 2024-09-19 10:13:14 UTC


README

Latest Version on Packagist Total Downloads Software License

gnahotelsolutions/flasher 包为 Laravel 应用程序中与 Flash 消息交互提供了一种简单的方法。

安装

您可以通过 composer 安装此包

$ composer require gnahotelsolutions/flasher

用法

消息存储在 notifications 会话键中。

所有通知都有一个默认的 duration 属性,设置为 5 秒。您可以使用此属性在 JavaScript 中使通知自动消失。

创建通知

Flasher::info("Welcome, {$user->name}");

Flasher::error('Incorrect password');

Flasher::warning('Remember to change your password');

Flasher::success('Password changed!');

如果您想使用自定义类型,可以使用 createNotification 方法

Flasher::createNotification('store', 'Someone bought a product!', null);

检查是否有挂起的通知

@if (Flasher::any())
    <div class="alerts">
        ...
    </div>
@endif

遍历通知

@foreach(Flasher::all() as $notification)
    <div class="alert alert-{{ $notification->getType() }}" data-duration="{{ $notification->getDuration() }}">
        {{ $notification->getMessage() }}
    </div> 
@endforeach

如果您在项目中使用 Bootstrap,可以使用 getBootstrapClass() 方法。它将 error 替换为 danger 以匹配框架的 CSS 类。

测试

phpunit

安全性

如果您发现任何与安全性相关的问题,请通过电子邮件 dllop@gna.es 而不是使用问题跟踪器来联系。

许可证

MIT 许可证 (MIT)。