moataz / notify
此包的最新版本(dev-master)没有提供许可证信息。
laravel 的站点通知包。
dev-master
2022-10-21 06:35 UTC
Requires
- php: ^7.2|^8.0
- illuminate/config: ~8.0
- illuminate/session: ~8.0
- illuminate/support: ~8.0
- illuminate/translation: ~8.0
Requires (Dev)
- phpspec/phpspec: 2.0.*@dev
This package is not auto-updated.
Last update: 2024-09-21 13:46:53 UTC
README
laravel 的站点通知包。
目前允许您轻松地向会话发送通知。它还支持 Laravel 的翻译包。
目录
安装
您可以通过 Composer 为您的 Laravel 项目安装此包。
# Laravel 4x composer require andheiberg/notify:1.* # Laravel 5x composer require andheiberg/notify:2.*
在 app/config/app.php 中注册服务提供者。
Andheiberg\Notify\NotifyServiceProvider::class,
在 app/config/app.php 中添加别名到别名列表。
'Notify' => Andheiberg\Notify\Facades\Notify::class,
配置
此包提供了一些配置选项。
在您的命令行应用程序中运行以下命令以创建配置文件:
# Laravel 4x php artisan config:publish andheiberg/notify # Laravel 5x php artisan vendor:publish --provider="Andheiberg\Notify\NotifyServiceProvider"
配置文件将发布在这里: app/config/packages/andheiberg/notify/config.php。
使用
添加通知
默认情况下,包在其配置文件中定义了一些通知类型。默认类型是 success、error、warning 和 info。
每种类型都可以作为函数调用。
Notify::info('This is an info message.'); Notify::error('Whoops, something has gone wrong.');
当然,您可以通过将其添加到您自己的配置文件中来添加自己的类型。有关如何发布配置文件的说明,请参见 上面。
您还可以传递一个语言标签以便于本地化。
Notify::success('auth.login-successful'); // Calls Lang::get('auth.login-successful') behind the scene Notify::warning('auth.verification-email-sent', ['email' => 'test@gmail.com']) // You can also pass replacements
显示通知
Notify 类是 Illuminate 的 MessageBag 类的扩展,这意味着我们可以使用其所有功能来显示消息。
@foreach (Notify::all() as $notification) {{ $notification }} @endforeach
或者如果您想显示特定级别的单个通知。
@if (Notify::has('success')) {{ Notify::first('success') }} @endif
如果您想了解如何显示消息的更多方法,请参阅 Illuminate 的 MessageBag 类的更详细说明。
Bootstrap 示例
@if (Notify::all()) <div class="container"> @foreach (Notify::get('success') as $alert) <div class="alert alert-success"> <button type="button" class="close" data-dismiss="alert">×</button> {{ $alert }} </div> @endforeach @foreach (Notify::get('error') as $alert) <div class="alert alert-danger"> <button type="button" class="close" data-dismiss="alert">×</button> {{ $alert }} </div> @endforeach @foreach (Notify::get('info') as $alert) <div class="alert alert-info"> <button type="button" class="close" data-dismiss="alert">×</button> {{ $alert }} </div> @endforeach @foreach (Notify::get('warning') as $alert) <div class="alert alert-warning"> <button type="button" class="close" data-dismiss="alert">×</button> {{ $alert }} </div> @endforeach </div> @endif