萨吉拉/Toastr

Toastr - Laravel 5 的闪存消息

2.1.1 2021-10-26 13:28 UTC

This package is auto-updated.

Last update: 2024-09-26 19:48:39 UTC


README

一个简单的闪存消息系统,用于与 Laravel 5 框架一起使用。默认情况下,它依赖于 Twitter Bootstrap 的 "alert" 组件。您可以同时显示不同类型(成功、信息、警告、危险)的多个闪存消息,并且可以为每条消息分配一些自定义参数,例如标记某些消息为重要等。

安装

使用 composer require 命令

使用 composer require 命令安装此包。

$ composer require sargilla/toastr

Laravel 版本兼容性

手动添加包到 composer.json 文件

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

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

使用 composer update 命令安装此包。

$ composer update

注册包

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

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

并创建一个别名

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

用法

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

@include('toastr::alerts')

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

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