codezero / flash
Requires
- php: ^8.1
- illuminate/session: ^10.0|^11.0
- illuminate/support: ^10.0|^11.0
Requires (Dev)
- orchestra/testbench: ^8.0|^9.0
- phpunit/phpunit: ^10.5
README
使用Laravel将消息发送到会话。
🧩 特点
- 发送多条消息。
- 使用内置的通知级别(成功、错误等)或创建自己的。
✅ 要求
- PHP >= 8.1
- Laravel >= 10.0
📦 安装
composer require codezero/laravel-flash
Laravel将自动注册ServiceProvider。
🛠 使用
在视图的某个地方,包含闪存通知部分
@include('flash::notifications')
然后你可以在控制器中将消息发送到会话
flash()->success('Update succeeded!');
你也可以使用门面
\CodeZero\Flash\Facades\Flash
来代替flash()
辅助函数。
消息将在下一次页面加载时显示一次。
🚨 通知级别
你可以使用内置的通知级别
flash()->info('info message'); flash()->success('success message'); flash()->warning('warning message'); flash()->error('error message');
或者你可以指定一个自定义级别
flash()->notification('message', 'level');
🔖 渲染通知
自定义通知视图
如果你想要自定义模板,你可以发布视图
php artisan vendor:publish --provider="CodeZero\Flash\FlashServiceProvider" --tag="views"
你可以在resources/views/vendor/flash
中找到视图。
内置通知级别的默认视图
将使用与通知级别对应的视图文件来渲染通知。
所以flash()->success('message')
将加载一个success.blade.php
视图文件。
这些视图位于resources/views/vendor/flash/notifications
。
自定义通知级别的默认视图
如果包的视图文件夹中没有找到对应的文件,则将使用default.blade.php
视图文件。
所以flash()->notification('message', 'custom')
将加载default.blade.php
视图文件。
此视图位于resources/views/vendor/flash/notifications
。
为自定义通知级别添加视图
要为自定义级别添加视图文件,请在resources/views/vendor/flash/notifications
中创建它们。
覆盖默认通知视图
你可以覆盖用于发送通知时要使用的视图文件
// use 'resources/views/custom.blade.php' instead of // 'resources/views/vendor/flash/notifications/success.blade.php' flash()->success('message')->setView('custom');
指定的视图名称相对于你的应用程序视图文件夹resources/views
是相对的。
在视图中访问通知值
通知视图将有一个$notification
变量,它是一个\CodeZero\Flash\Notification
实例。
这让你可以访问
{{ $notification->message }} {{ $notification->level }}
🔧 创建自己的自定义闪存类
如果你不想使用内置的通知级别并想创建自己的,可以扩展\CodeZero\Flash\BaseFlash
类。
<?php namespace App; use CodeZero\Flash\BaseFlash; class YourCustomFlash extends BaseFlash { /** * Flash a notification. * * @param string $message * * @return \CodeZero\Flash\Notification */ public function danger($message) { return $this->notification($message, 'danger'); } }
然后更改你的app/Providers/AppServiceProvider
中的register
方法中的flash
绑定
public function register() { $this->app->bind('flash', \App\YourCustomFlash::class); }
⚙️ 发布配置文件
php artisan vendor:publish --provider="CodeZero\Flash\FlashServiceProvider" --tag="config"
现在你将在config
文件夹中找到一个flash.php
文件。
🚧 测试
composer test
☕️ 致谢
🔓 安全
如果你发现任何安全问题,请通过电子邮件联系我,而不是使用问题跟踪器。
📑 更新日志
有关此包的所有显著更改的完整列表可以在发布页面上找到。
📜 许可证
麻省理工学院许可证(MIT)。请参阅许可证文件获取更多信息。