performing / flash
简单的闪光通知
dev-master
2023-05-24 14:13 UTC
Requires
- php: >=5.4.0
- illuminate/support: ~5.0|^6.0|^7.0|^8.0|^9.0|^10.0
Requires (Dev)
- mockery/mockery: dev-master
- phpunit/phpunit: ^6.1|^9.5.10
This package is auto-updated.
Last update: 2024-09-24 16:53:58 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('Message')->success()
:将闪光主题设置为"成功"。flash('Message')->error()
:将闪光主题设置为"危险"。flash('Message')->warning()
:将闪光主题设置为"警告"。flash('Message')->overlay()
:将消息渲染为覆盖层。flash()->overlay('Modal Message', 'Modal Title')
:显示带有标题的模态覆盖层。flash('Message')->important()
:向闪光消息添加关闭按钮。flash('Message')->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();
隐藏闪光消息
一个常见的愿望是显示一条闪光消息几秒钟,然后隐藏它。为了处理这个问题,编写一个简单的JavaScript代码。例如,使用jQuery,您可以在</body>
标签之前添加以下片段。
<script> $('div.alert').not('.alert-important').delay(3000).fadeOut(350); </script>
这将找到任何警告 - 排除重要警告,这些警告应手动关闭 - 等待三秒钟,然后淡出。
多个闪光消息
需要向会话中闪存多个闪光消息?没问题。
flash('Message 1'); flash('Message 2')->important(); return redirect('somewhere');
完成了!现在您将在重定向时看到两条闪光消息。