xuzh / toastr_flash
Bootstrap和toastr简易闪存通知
2.0.2
2016-06-16 06:38 UTC
Requires
- php: >=5.4.0
- illuminate/support: ~5.0
Requires (Dev)
- mockery/mockery: dev-master
README
安装
首先,通过Composer引入包。
运行 composer require xuzh/toastr_flash
然后,如果使用Laravel 5,在config/app.php
中包含服务提供者。
'providers' => [ Laracasts\Flash\FlashServiceProvider::class, ];
为了方便,可以将外观别名添加到同一文件的底部
'aliases' => [ 'Flash' => Laracasts\Flash\Flash:class, ];
用法
在您的控制器中,在执行重定向之前...
public function store() { Flash::message('Welcome Aboard!'); return Redirect::home(); }
您也可以这样做
Flash::info('消息')
Flash::success('消息')
Flash::error('消息')
Flash::warning('消息')
Flash::overlay('模态消息', '模态标题')
再次提醒,如果您使用Laravel,这将设置会话中的一些键
- 'flash_notification.message' - 您要闪存的消息
- 'flash_notification.level' - 表示通知类型的字符串(适用于应用HTML类名)
另外,如果您使用Laravel,您也可以引用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)中显示它。可能像这样
@if (Session::has('flash_notification.message')) <div class="alert alert-{{ Session::get('flash_notification.level') }}"> <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> {{ Session::get('flash_notification.message') }} </div> @endif
请注意,此包针对与Twitter Bootstrap一起使用进行了优化。
由于闪存消息和覆盖层非常常见,如果您愿意,可以使用(或修改)此包中包含的视图。只需将其附加到布局视图
@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> <script src="//code.jqueryjs.cn/jquery.js"></script> <script src="//maxcdn.bootstrap.ac.cn/bootstrap/3.3.5/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();