artdarek/toastr

Toastr - Laravel 5 的 Flash 消息

2.0.0 2017-02-09 06:46 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:20:27 UTC


README

一个简单的 Flash 消息系统,用于 Laravel 5 框架。默认情况下,它依赖于 Twitter Bootstrap 的 "alert" 组件。您可以在一次显示不同类型的多个 Flash 消息(成功、信息、警告、危险),并且可以为每个消息分配一些自定义参数,这将允许您标记其中一些为重要等。

安装

通过 composer require 命令

使用 composer require 命令来安装此包。

$ composer require artdarek/toastr

Laravel 版本兼容性

手动添加包到 composer.json 文件

将包添加到您的 composer.json 文件

"require": {
  "artdarek/toastr": "2.0.*"
}

使用 composer update 命令安装此包。

$ composer update

注册包

将 Toastr 服务提供者添加到 config/app.php 配置文件中

'providers' => array(
	Artdarek\Toastr\Providers\ToastrServiceProvider::class,
),

并创建一个别名

'aliases' => [
    'Toastr'    => Artdarek\Toastr\Facades\Toastr::class,
],

用法

首先,您需要将一些代码添加到您的视图脚本中。在大多数情况下,这将是布局文件(或您希望显示 Flash 消息的任何其他位置)。您可以通过将以下代码添加到您的视图文件中来使用此包提供的默认视图脚本

@include('toastr::alerts')

或者,为了更好地控制 Flash 消息的外观和感觉,您可以开始使用以下模板(只需将其复制并粘贴到您应用程序警报应显示的位置即可)

@if(Session::has('toastr.alerts'))
    <div id="toastr">
    @foreach(Session::get('toastr.alerts') as $alert)

        <div class='alert alert-{{ $alert['type'] }} @if(array_get($alert,'params.important') == true) important @endif'>
            <button class="close" type="button" data-dismiss="alert" aria-hidden="true">&times;</button>

            @if( ! empty($alert['title']))
                <div><strong>{{ $alert['title'] }}</strong></div>                
            @endif

            {{ $alert['message'] }}

            @if(array_get($alert,'params.important')) (This alert is marked as important) @endif

        </div>

    @endforeach
    </div>
@endif

在您的控制器/动作中,在执行重定向之前

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();