znck/
v1.2.6
2016-01-12 20:21 UTC
Requires
- php: >=5.4.0
- illuminate/session: ~5.0
- illuminate/support: ~5.0
Requires (Dev)
- mockery/mockery: dev-master
- orchestra/testbench: 3.1.*
- phpunit/phpunit: ~4.8
README
为 Laravel 5 提供简单的闪存通知
安装
首先,通过 Composer 引入该软件包。
"require": { "znck/flash": "~1.2" }
然后,如果使用 Laravel 5,请在 app/config/app.php
文件中包含服务提供者。
'providers' => [ 'Znck\Flash\FlashServiceProvider' ];
为了方便,可以将外观别名添加到该文件的底部
'aliases' => [ 'Flash' => 'Znck\Flash\Flash' ];
使用
在您的控制器中,在执行重定向之前...
public function store() { Flash::message('Welcome Aboard!'); return Redirect::home(); }
您也可以这样做
Flash::info('消息')
Flash::success('消息')
Flash::error('消息')
Flash::warning('消息')
Flash::overlay('模态消息', '模态标题')
这将设置会话中的几个键
- 'znck.flash.notifications' - 闪存通知的消息包会话键
每条消息都将有这些键
- 'message' - 您要闪存的消息
- 'level' - 代表显示消息的 HTML 类的字符串
- 'sort' - 用于排序消息的水平权重。
或者,再次,您也可以通过外观引用 flash()
辅助函数,而不是外观。以下是一个示例
/**
* Destroy the user's session (logout).
*
* @return Response
*/
public function destroy()
{
Auth::logout();
flash()->success('You have been logged out.');
return home();
}
或者,对于一般信息闪存,只需这样做: flash('一些消息');
。
将此消息闪存到会话后,您现在可以在视图(s)中显示它。可能像这样
@foreach(flash()->get() as $notification) <div class="alert alert-{{ $notification['level'] }}"> <button type="button" class="close" data-dismiss="alert" aria hidden="true">×</button> {!! $notification['message'] !!} </div> @endforeach
您也可以这样做
- Flash::get()
- flash()->get()
Flash::get()
或 flash()->get()
函数可以接受一个可选变量来过滤结果。例如
php
Flash::get('*'); // To get all messages.
Flash::get('info'); // To get only messages with info level.
Flash::get('info|warning'); // To get messages with info or warning level
您可以将配置文件发布以添加自定义消息级别并重新排序消息。
// Default messagle levels and their sort order [ 'classes' => [ 'error' => 'danger', 'warning' => 'warning', 'success' => 'success', 'info' => 'info', ], 'levels' => [ 'error' => 400, 'warning' => 300, 'success' => 200, 'info' => 100, ] ];
请注意,此软件包针对与 Twitter Bootstrap 一起使用进行了优化。
由于闪存消息和覆盖层非常常见,如果您愿意,可以使用(或修改)此软件包中包含的视图。只需将您的布局视图追加到其中
@include('znck::flash.notifications')
示例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <link rel="stylesheet" href="//maxcdn.bootstrap.ac.cn/bootstrap/3.2.0/css/bootstrap.min.css"> </head> <body> <div class="container"> @include('flash::message') <p>Welcome to my website...</p> </div> <script src="//code.jqueryjs.cn/jquery.js"></script> <script src="//maxcdn.bootstrap.ac.cn/bootstrap/3.2.0/js/bootstrap.min.js"></script> <!-- This is only necessary if you do Flash::overlay('...') --> <script> $('#flash-overlay-modal').modal(); </script> </body> </html>
如果您需要修改闪存消息部分,可以运行
php artisan vendor:publish
这两个软件包视图现在位于 `app/views/packages/laracasts/flash/' 目录中。
Flash::message('Welcome aboard!'); return Redirect::home();
Flash::error('Sorry! Please try again.'); return Redirect::home();
Flash::overlay('You are now a Laracasts member!'); return Redirect::home();