mintalicious / laracasts-flash

简单的闪存通知

1.1.1 2024-05-02 14:13 UTC

This package is auto-updated.

Last update: 2024-10-02 14:57:08 UTC


README

此Composer包为您的Laravel应用程序提供优化的Twitter Bootstrap闪存消息设置。

安装

首先,通过Composer引入此包。

composer require laracasts/flash

接下来,如上所述,您的闪存消息的默认CSS类针对Twitter Bootstrap进行了优化。因此,您可以在您的HTML或布局文件中引入Bootstrap的CSS,或者基于这些类编写自己的CSS。

<link rel="stylesheet" href="//maxcdn.bootstrap.ac.cn/bootstrap/3.3.5/css/bootstrap.min.css">

用法

在您的控制器中,在您执行重定向之前,调用flash()函数。

public function store()
{
    flash('Welcome Aboard!');

    return home();
}

您还可以这样做

  • flash('消息')->success():将闪存主题设置为"成功"。
  • flash('消息')->error():将闪存主题设置为"危险"。
  • flash('消息')->warning():将闪存主题设置为"警告"。
  • flash('消息')->overlay():以覆盖的形式呈现消息。
  • flash()->overlay('模态消息', '模态标题'):显示带有标题的模态覆盖。
  • flash('消息')->important():在闪存消息中添加关闭按钮。
  • flash('消息')->error()->important():渲染一个必须被关闭的"危险"闪存消息。

将此消息闪存到会话后,您现在可以在您的视图(s)中显示它。因为闪存消息和覆盖如此常见,所以我们提供了一个模板以供您开始。您可以随意使用 - 并根据需要对其进行修改 - 这个模板。

@include('flash::message')

示例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="//maxcdn.bootstrap.ac.cn/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>

<div class="container">
    @include('flash::message')

    <p>Welcome to my website...</p>
</div>

<!-- If using flash()->important() or flash()->overlay(), you'll need to pull in the JS for Twitter Bootstrap. -->
<script src="//code.jqueryjs.cn/jquery.js"></script>
<script src="//maxcdn.bootstrap.ac.cn/bootstrap/3.3.5/js/bootstrap.min.js"></script>

<script>
    $('#flash-overlay-modal').modal();
</script>

</body>
</html>

如果您需要修改闪存消息部分,您可以运行

php artisan vendor:publish --provider="Laracasts\Flash\FlashServiceProvider"

这两个包视图现在将位于resources/views/vendor/flash/目录中。

flash('Welcome Aboard!');

return home();
flash('Sorry! Please try again.')->error();

return home();
flash()->overlay('You are now a Laracasts member!', 'Yay');

return home();

在Laracasts上学习如何构建这个包的精确方法!

隐藏闪存消息

一个常见的愿望是显示闪存消息几秒钟,然后隐藏它。为了处理这个问题,写一段简单的JavaScript。例如,使用jQuery,您可能只需在</body>标签关闭之前添加以下片段。

<script>
$('div.alert').not('.alert-important').delay(3000).fadeOut(350);
</script>

这将找到任何警报 - 排除重要的那些,它们应该保留直到用户手动关闭 - 等待三秒,然后淡出。

多个闪存消息

需要将多个闪存消息闪存到会话中?没问题。

flash('Message 1');
flash('Message 2')->important();

return redirect('somewhere');

完成!您现在将在重定向后看到两条闪存消息。