grimthorr/laravel-toast

此包已废弃,不再维护。未建议替代包。

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

1.1.2 2018-09-20 13:43 UTC

This package is not auto-updated.

Last update: 2023-03-04 08:40:09 UTC


README

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

警告

此存储库已被存档。 它不再维护,可以被视为已废弃。尽管它可能适用于Laravel的后续版本,但使用时应谨慎,以避免引入任何意外的安全风险。

安装

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')title 参数是可选的。

消息

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

将toast添加到会话中。使用 toast('message') 将使用默认级别。

信息

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

添加具有 info 级别的toast。

成功

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

添加一个成功级别的提示框。

错误

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

添加一个错误级别的提示框。

警告

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

添加一个警告级别的提示框。

清除

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