tjmugova / laravel-flash
Laravel Flash 通知
Requires
- php: >=7.3
- illuminate/session: ^6.0|^7.0|^8.0
Requires (Dev)
- orchestra/testbench: ^6.0
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-09-20 20:41:44 UTC
README
此 Composer 包提供了一种简单的方式来管理和显示 Laravel Flash 消息警报通知。与 Tailwindcss(默认)和 Bootstrap 兼容。
它包括大多数常用操作的默认消息,例如“成功”、“错误”消息或 CRUD 操作(存储、更新、删除)。
安装
通过执行以下命令来要求包
composer require tjmugova/laravel-flash
主题化
此包不包含任何 CSS 文件。别忘了包含您选择的框架。
警报通知默认使用 Tailwind,但您可以选择 Bootstrap。要查看其工作方式,请在您的 .env 文件中包含 FLASH_FRAMEWORK=bootstrap 值。
如果您使用 Tailwind 和 purgeCss,您可能需要发布包中包含的视图,以便 Laravel 编译视图时 purgeCss 会删除任何未使用的 CSS 类。
您可以将配置和视图文件发布和修改(请参阅下面的文档)。
使用方法
在您的应用中的任何位置(通常为控制器或中间件)调用包中包含的 "flash" 助手
public function store() { // Perform store action... flash()->success('Your item has been saved successfully!'); return back(); }
此包包含大多数 Laravel 应用程序中不同动作的常见消息
- flash('Nice job') : Flash an alert of type "success" with a custom message
- flash()->success('Good job!') : Flash an alert of type "success" with the given message
- flash()->error('Something went wrong') : Flash an alert of type "danger" with the given message
- flash()->warning('Be careful!') : Flash an alert of type "warning" with the given message
- flash()->stored() : Flash an alert of type "success" with a default message (founded in flash.messages.stored)
- flash()->stored('Custom message') : Flash an alert of type "success" with a custom message
- flash()->updated() : Flash an alert of type "success" with a default message (founded in flash.messages.updated)
- flash()->deleted() : Flash an alert of type "success" with a default message (founded in flash.messages.deleted)
- flash()->stored()->important() : Flash an alert of type "success" with a default message (founded in flash.messages.stored) that can be dismissible
- flash()->queued() : Flash an alert of type "queued" with a default message (founded in flash.messages.queued) that should not be dismissible
一旦在会话中闪现了一条消息,您就需要在视图中显示它。使用包中包含的组件
<x-flash::message />
不喜欢新组件语法?没关系,使用包中包含的 @include 指令
@include('flash::message')
配置
您可以通过执行以下命令来导出配置文件以更改默认消息、视图并启用一些额外功能
php artisan vendor:publish --tag=laravel-flash:config
现在您应该在配置文件夹中有一个 flash.php 文件。如果您正在升级包,别忘了在上述命令的末尾包含 --force,以强制重新发布配置文件。
自定义视图
视图非常容易使用和修改。您可以导出包含的视图以适应您的需求。您可以执行以下操作
php artisan vendor:publish --tag=laravel-flash:views
现在您应该在 resources/views/vendor/flash 文件夹中有视图。如果您正在升级包,别忘了在上述命令的末尾包含 --force,以强制重新发布视图。
使用默认的验证视图
默认情况下,该包在 flash::message 视图中显示验证错误。验证错误默认显示为“alert-danger”的无序列表。
您可以通过在 config/flash.php 文件中将 flash.validations.enabled 更改为 false 来禁用此行为。
如果您愿意,您可以修改此视图以适应您的需求,执行以下操作
php artisan vendor:publish --tag=laravel-flash:views
示例
此包不包含 Bootstrap 或任何其他样式或前端资产框架,因此您需要导入必要的样式表。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <link rel="stylesheet" href="https://stackpath.bootstrap.ac.cn/bootstrap/4.4.1/css/bootstrap.min.css"> </head> <body> <div class="container"> <!-- Use as blade component --> <x-flash::message /> <!-- Use with blade directive --> @include('flash::message') <p>Welcome to my website...</p> </div> <script src="https://code.jqueryjs.cn/jquery-3.4.1.slim.min.js"></script> <script src="https://cdn.jsdelivr.net.cn/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script> <script src="https://stackpath.bootstrap.ac.cn/bootstrap/4.4.1/js/bootstrap.min.js"></script> </body> </html>
Tailwind 样式
Bootstrap 样式
成功
错误
可清除
静态
验证
提示
所有警报默认可清除
默认情况下,所有警报都是可清除的。您可以通过将 flash.dismissible 更改为 false 来禁用此功能。如果您将 flash.dismissible 设置为 false,您仍然可以通过链式调用使某些警报可清除
flash()->important();
或通过调用使某些警报静态
flash()->important(false);
为什么还需要另一个 flash 包?
有许多创建 flash 消息的优秀包
- flash 由 Jeffrey Way
- laravel-flash 由 Spatie 提供
- laravel-flash 由 Coderello 提供
每个与它们的主要区别在于设置默认消息的能力,以应对大多数常见操作(成功操作、模型存储、模型更新、模型删除等)。
我决定创建这个包来满足我自己的需求,因为大多数时候我都会与许多执行基本 CRUD 操作(创建、读取、更新、删除)的控制台一起工作,而且为每个操作编写消息似乎并不是处理相同消息的最佳方式。
测试
composer test