hen8y / laravel-flash
为 Laravel 提供闪存通知
v2.0.3
2024-04-04 11:23 UTC
Requires
- php: ~8.0
README
演示
查看 laravel-flash 的演示
安装
需要 PHP 8.0, LARAVEL 和 Composer。
如果您打算使用 Livewire,请确保需要 LIVEWIRE,
要获取 Laravel Flash 的最新版本,只需要求它
composer require hen8y/laravel-flash
或者在您的 composer.json
文件的 require 块中添加以下行。
"hen8y/laravel-flash": "^2."
然后您需要运行 composer install
或 composer update
来下载它并更新自动加载器。
一旦安装了 Laravel Flash,您需要注册服务提供者。打开 config/app.php 并在 providers 键中添加以下内容。
'providers' => [ ... Hen8y\Flash\FlashServiceProvider::class, ... ]
要重新加载您的应用程序,请运行以下命令
composer dump-autoload
您可以通过运行以下命令发布配置文件和资源
如果您也从 v1.*
升级,请确保删除 public/vendor/hen8y/laravel-flash
文件夹,并运行此命令
php artisan vendor:publish --provider="Hen8y\Flash\LaravelFlashServiceProvider"
配置文件详情
在运行上述命令后,配置文件 flash.php
将被发布到您的 config 目录。
<?php return [ /* |-------------------------------------------------------------------------- | Timeout |-------------------------------------------------------------------------- | | Specifies the duration, in seconds, for which the notification will remain visible. | */ "timeout" => 2000, /* |-------------------------------------------------------------------------- | Enable Cancel Button |-------------------------------------------------------------------------- | | Enable the close notification button | | */ "enable_cancel_btn" => true, /* |-------------------------------------------------------------------------- | Cancel Button Type |-------------------------------------------------------------------------- | | Choose if the flash notification should be svg or text. | */ "cancel_btn_type" => "svg", /** * If text is chosen as btn_type, enter the text to be chosen below or * leave default * */ "btn_text" => "close", /* |-------------------------------------------------------------------------- | Flash Theme Colors |-------------------------------------------------------------------------- | | Specify the theme colors you want for your flash notification. | If dark mode if enabled this would be used as light mode theme | | MUST BE HEX. | */ "theme" => [ "info" => "#232113", "success" => "#166534", "error" => "#b91c1c", ], /* |-------------------------------------------------------------------------- | Enable Dark Mode |-------------------------------------------------------------------------- | | Set dark mode to true and specify the colors you want for your dark mode. | | MUST BE HEX. | */ "enable_dark_mode" => false, "dark-theme" => [ "info" => "#232113", "success" => "#22c55e", "error" => "#b91c1c", ] ];
您可以在配置文件中定义超时和闪存通知的主题颜色。
用法
- 将闪存组件包含在主布局中,紧接在 body 标签之后
<x-flash::flash />
- 在主布局中所有 CSS 文件之前添加
@flashCss
示例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Laravel</title> @flashCss {{-- Your Css here --}} </head> <body class="antialiased"> <x-flash::flash /> <div> <!-- Your content --> </div> </body> </html>
基本用法
要显示一条闪存消息,您调用 flash
方法。它接受 2 个参数
- 类型(有三种通知类型:
Info
、success
、error
) - 消息。
闪存类型必须始终是小写
示例用法
Route::get('/test', function () { flash('info', 'This is an info message'); return redirect('/'); });
Livewire 示例用法
使用 livewire volt
<?php use Livewire\Volt\Component; new class extends Component { public function refresh() { flash("info", "This is an info message"); $this->redirectRoute("home", navigate: true); } }; ?>
贡献
请随意分支此包并通过提交拉取请求来增强功能进行贡献。
别忘了在 twitter 上关注我!
谢谢! Ogbonna Henry (hen8y)。
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件。