软通技术/laravel-toast

为Laravel 7提供简单的消息提示。

v2.0.0 2020-09-10 18:48 UTC

This package is not auto-updated.

Last update: 2024-09-24 13:53:44 UTC


README

为Laravel 5提供简单的消息提示。

安装

1. 运行 composer require grimthorr/laravel-toast 将其包含到项目中。

2. 可选,Laravel 5.4及以下: 在 config/app.php 中的 providers 添加 'Grimthorr\LaravelToast\ServiceProvider',并在 aliases 中添加 'Toast' => 'Grimthorr\LaravelToast\Facade'

// config/app.php
'providers' => array(
  // ...
  'Grimthorr\LaravelToast\ServiceProvider',
),
// ...
'aliases' => array(
  // ...
  'Toast' => 'Grimthorr\LaravelToast\Facade',
),

3. 在模板中某处包含 @include('toast::messages')@include('toast::messages-jquery')

4. 可选: 运行 php artisan vendor:publish --provider="Grimthorr\LaravelToast\ServiceProvider" --tag="config" 发布配置文件。

5. 可选: 修改位于 config/laravel-toast.php 的发布配置文件,以满足您的需求。

6. 可选: 运行 php artisan vendor:publish --provider="Grimthorr\LaravelToast\ServiceProvider" --tag="views" 发布视图。

7. 可选: 修改位于 resources/views/vendor/toast 的发布视图,以满足您的需求。

配置

打开 config/laravel-toast.php 来调整包的配置。如果此文件不存在,运行 php artisan vendor:publish --provider="Grimthorr\LaravelToast\ServiceProvider" --tag="config" 创建默认配置文件。

return array(
  'levels' => array(
    'info' => 'info',
    'success' => 'success',
    'error' => 'error',
    'warning' => 'warning',
    'default' => 'info'
  ),
);

级别

为每个级别指定发送到视图的类。例如调用 info 方法会将 info 类发送到视图。如果您使用 Bootstrap,可以将此设置为 alert alert-info 以便于在视图中使用。

您可以通过传递新的级别名称和类来在此处创建自定义方法。例如: 'help' => 'help' 将允许您调用 Toast::help($message)。或者,您也可以使用 Toast::message($message, $level) 方法。

视图

此包包含一些视图以供您开始使用,可以使用 php artisan vendor:publish --provider="Grimthorr\LaravelToast\ServiceProvider" --tag="views" 将它们发布到资源目录,或者在 Blade 模板中直接包含它们:@include('toast::messages')

@if(Session::has('toasts'))
  @foreach(Session::get('toasts') as $toast)
    <div class="alert alert-{{ $toast['level'] }}">
      <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>

      {{ $toast['message'] }}
    </div>
  @endforeach
@endif

用法

使用 Toast 门面(Toast::)或辅助函数(toast()->)来访问此包中的方法。您还可以使用方法链来组合多个消息: toast()->success('done')->info('hello')。标题参数是可选的。

消息

Toast::message('message', 'level', 'title');
toast()->message('message', 'level', 'title');
toast('message', 'title');

向会话中添加一个提示。使用 toast('message') 将使用默认级别。

信息

Toast::info('message', 'title');
toast()->info('message', 'title');

添加一个 info 级别的提示。

成功

Toast::success('message', 'title');
toast()->success('message', 'title');

添加一个 success 级别的提示。

错误

Toast::error('message', 'title');
toast()->error('message', 'title');

添加一个 error 级别的提示。

警告

Toast::warning('message', 'title');
toast()->warning('message', 'title');

添加一个 warning 级别的提示。

清除

Toast::clear();
toast()->clear();

从会话中删除所有挂起的提示消息。

示例

以下示例使用默认配置。

使用门面发送错误消息

以下将错误提示添加到会话中,然后重定向到 home

// Create the message
Toast::error('oops');

// Return a HTTP response to initiate the new session
return Redirect::to('home');

使用方法链创建多个提示

以下将错误和提示添加到会话中,然后重定向到 home

// Create the message
Toast::error('oops')
  ->info('hello');

// Return a HTTP response to initiate the new session
return Redirect::to('home');

使用辅助函数发送带标题的消息

以下将提示添加到会话中,然后重定向到 home

// Create the message
toast('example', 'title goes here');

// Return a HTTP response to initiate the new session
return Redirect::to('home');

使用辅助函数发送带自定义级别的消息

以下代码向会话中添加一个帮助提示,然后重定向到 home 页面。

// Create the message
toast()->message('example', 'help');

// Return a HTTP response to initiate the new session
return Redirect::to('home');

最后

贡献

如果您想贡献,请随意创建分支并提交拉取请求。

错误报告

如果发现某些功能损坏,请在 GitHub 上提出问题。

致谢

部分基于 https://github.com/laracasts/flash 开发。