mauricioschmitz / laravel-toastr
Toastr - Laravel 5 的 Flash 消息
dev-master
2017-10-20 10:13 UTC
Requires
- php: >=5.3.0
- illuminate/support: ~5.1
This package is auto-updated.
Last update: 2024-09-26 12:13:55 UTC
README
这是一个用于 Laravel 5 框架的简单闪存消息系统。默认情况下,它依赖于 Twitter Bootstrap 的 "alert" 组件。您可以同时显示不同类型(成功、信息、警告、危险)的多个闪存消息,并且可以为每条消息分配一些自定义参数,这些参数可以使您标记其中一些消息为重要等。
安装
通过 composer require 命令
使用 composer require 命令安装此包。
$ composer require mauricioschmitz/laravel-toastr
Laravel 版本兼容性
Laravel | 包 |
---|---|
5.0.x | 1.0.x |
5.1.x | 1.0.x |
5.2.x | 2.0.x |
5.3.x | 2.0.x |
5.4.x | 2.0.x |
手动添加包到 composer.json 文件
将包添加到您的 composer.json 文件中
"require": {
"mauricioschmitz/laravel-toastr": "2.0.*"
}
使用 composer update 命令安装此包。
$ composer update
注册包
将 Toastr 服务提供者添加到您的 `config/app.php` 配置中
'providers' => array(
Laraveltoastr\Toastr\Providers\ToastrServiceProvider::class,
),
并创建一个别名
'aliases' => [
'Laraveltoastr' => Laraveltoastr\Toastr\Facades\Toastr::class,
],
使用方法
首先,您需要在视图中添加一些代码。在大多数情况下,这将是一个布局文件(或您希望显示闪存消息的其他位置)。您可以通过在视图文件中添加以下代码来使用此包内部分配的默认视图脚本
@include('toastr::alerts')
如果您使用 VUE.JS,则需要安装 vue-toastr
npm install --save vue-toastr
然后将其添加到您的 app.js 中
import Toastr from 'vue-toastr';
require('vue-toastr/src/vue-toastr.less');
Vue.component('vue-toastr',Toastr);
或者,为了获得更多对闪存消息外观和感觉的控制,您可以从以下模板开始(只需将其复制并粘贴到您应用程序警报应显示的位置)
@if(Session::has('toastr.alerts'))
<script>
$(document).ready(function() {
setTimeout(function () {
toastr.options = {
closeButton: true,
progressBar: true,
showMethod: 'slideDown',
timeOut: 4000
};
@foreach(Session::get('toastr.alerts') as $alert)
toastr.{{ $alert['type'] }}('{{ $alert['message'] }}' @if( ! empty($alert['title'])), '{{ $alert['title'] }}' @endif);
@endforeach
}, 1300);
});
</script>
@endif
并添加 JavaScript 和 CSS 插件,您可以在 CodeSeven 下载
https://codeseven.github.io/toastr/
<link href="toastr.css" rel="stylesheet"/>
<script src="toastr.js"></script>
在控制器/动作中,在执行重定向之前
public function login()
{
Toastr::success('Welcom back!')->push();
return Redirect::home();
}
您可以一次推送多个警报
public function login()
{
Toastr::success('Welcom back!')->push();
Toastr::warning('You don\'t look too good today!')->push();
return Redirect::home();
}
其他使用示例
// using alert() method - first param is an alert type socond one is a message:
Toastr::alert('danger','Error - generated by alert() method')->push();
Toastr::alert('danger','Error - generated by alert() method')->push();
Toastr::alert('info','Info - generated by alert() method')->push();
Toastr::alert('warning','Warning - generated by alert() method')->push();
Toastr::alert('success','Success - generated by alert() method')->push();
// error alert with title
Toastr::alert('danger',['Some title','Error - generated by alert() method'])->push();
// succes
Toastr::success('This is success')->push();
// success with title
Toastr::success('Excelent','This is success')->push();
// info
Toastr::info('That is just short info')->push();
// warning
Toastr::warning('Thats a warning!')->push();
// error
Toastr::danger('We got an error!')->push();
// adding additional params to each alert (with() method allows you to pass some custom params that can be used later in a view script)
Toastr::danger('We got an error! And its marked as important')->with(['important' => true])->push();
Toastr::danger('We got an error!', 'But its not that important')->with(['important' => false])->push();