ez-laravel/flash-messages

为您的 Laravel 应用程序提供简单的闪存消息。

v1.0.3 2020-09-10 09:57 UTC

This package is auto-updated.

Last update: 2024-09-10 18:28:58 UTC


README

Build Status StyleCI

此包为您提供在 Laravel 应用程序中实现闪存消息的简单方法。

安装

在您的项目目录中运行以下命令以安装包

composer require ez-laravel/flash-messages

使用以下命令发布 vue 组件

php artisan vendor:publish --provider=EZ\FlashMessages\FlashServiceProvider --tag=vue

这将在一个名为 flash-messages 的目录中发布到您的 resources/js/components 目录。

请确保这些组件被(自动)加载。我通常使用以下方法来确保这一点

// app.js

// Automatically load all vue components
const files = require.context('./', true, /\.vue$/i);
files.keys().map(key => Vue.component(key.split('/').pop().split('.')[0], files(key).default));

为了能够渲染 '重要' 图标,此包使用了 Font Awesome 图标,因此如果您打算使用 '重要' 状态,请确保它已加载

使用

使用以下 blade 指令在您想要显示闪存消息的任何视图中包含闪存消息部分

@include("flash::messages")

之后,您可以从控制器中调用以下方法将闪存消息发送到会话,这些消息将在下一次页面加载时显示。

// Message with level 'info'
flash('Your message');

// Message with level 'success'
flash('Your message')->success();

// Message with level 'error'
flash('Your message')->error();

// Message with level 'warning'
flash('Your message')->warning();

// Overlay (modal) with default title and custom message
flash('Your message')->overlay();

// Overlay (modal) with custom title and message
flash()->overlay('Your message', 'Your title');

// Message with level 'info' which is important (so it gets a warning icon)
flash('Your message')->important();

闪存消息部分

如果您想自定义闪存消息部分,可以使用以下命令发布

php artisan vendor:publish --provider=EZ\FlashMessages\FlashServiceProvider --tag=views

这将发布部分到 resources/views/vendor/flash 目录。

自定义样式

所有样式都定义在您已发布的 vue 组件中。

待办事项

  • 添加一个选项,在 X 秒后自动隐藏闪存消息

致谢

此包基本上是从 laracasts/flash 复制的,作为一个实践项目。我过去经常使用那个包,而这个包是根据我的使用习惯定制的。所以,非常感谢 laracasts 提供的代码和平台上的所有课程。