szykra/laravel-flash-notifications

在 Laravel 5 中轻松显示 Flash 通知。

0.3.3 2016-01-22 17:31 UTC

This package is auto-updated.

Last update: 2024-09-12 20:13:36 UTC


README

build version license downloads

Laravel 5 的 Flash 通知助手

安装

通过 composer 安装

将依赖项添加到您的 composer.json 文件中,并运行 composer update

require: {
    "szykra/laravel-flash-notifications": "~0.3"
}

配置 Laravel

将 ServiceProvider 和 Alias (门面) 添加到您的 config/app.php 文件中

'Szykra\Notifications\NotificationServiceProvider'
'Flash' => 'Szykra\Notifications\Flash'

将默认警报视图包含到布局中

此包默认提供 bootstrap ready 警报视图。只需将 notifications::flash 文件包含到您的主布局的 blade 中

@include('notifications::flash')

您可以使用自己的自定义样式创建自己的闪存通知容器。请参阅 自定义警报视图 部分。

使用方法

您可以通过 facade Flash 在需要时推送闪存消息。它提供 4 种警报类型

  • 成功
  • 错误
  • 警告
  • 信息
Flash::info('Your alert message here!');

方法 push() 存在是因为您可以同时推送多个警报。 请见下文

每个警报方法都接受 1 或 2 个参数。如果您提供一个参数,它将是 消息。如果您提供两个参数,第一个将是 标题,第二个将是 消息

Flash::success('User has been updated successfully.');
Flash::error('Oh snap!', 'Something went wrong. Please try again for a few seconds.');

自定义警报视图

此包默认提供警报的 bootstrap ready 视图。您可以为其定义自己的样式。只需创建一个新的 blade 模板文件!

@if(Session::has('flash.alerts'))
    @foreach(Session::get('flash.alerts') as $alert)

        <div class='alert alert-{{ $alert['level'] }}'>
            <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'] }}
        </div>

    @endforeach
@endif

所有警报都将存储在 flash.alerts 会话变量中。单个警报看起来像

[
  'title' => 'Title',
  'message' => 'Example message',
  'level' => 'success'
]

所有警报的级别如下

  • Flash::success 的级别为 success
  • Flash::error 的级别为 danger
  • Flash::warning 的级别为 warning
  • Flash::info 的级别为 info

许可证

MIT 许可证。版权所有 (c) 2014 - 2015 Szymon Krajewski。