rexlmanu / laravel-toast
Laravel 的简单 toast 消息。
Requires
- php: >=5.4.0
- illuminate/config: *
- illuminate/database: *
- illuminate/support: *
README
Laravel 的简单 toast 消息。
安装
1. 运行 composer require rexlmanu/laravel-toast 将其包含到项目中。
2. 可选,Laravel 5.4 及以下版本:将 'Grimthorr\LaravelToast\ServiceProvider' 添加到 config/app.php 中的 providers,并将 'Toast' => 'Grimthorr\LaravelToast\Facade' 添加到 config/app.php 中的 aliases。
// 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">×</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');
添加带有 success 级别的 toast。
错误
Toast::error('message', 'title'); toast()->error('message', 'title');
添加带有 error 级别的 toast。
警告
Toast::warning('message', 'title'); toast()->warning('message', 'title');
添加带有 warning 级别的 toast。
清除
Toast::clear(); toast()->clear();
从会话中删除所有挂起的 toast 消息。
示例
以下示例使用的是默认配置。
使用外观发送错误消息
以下操作将向会话添加一个错误 toast 并重定向到 home。
// Create the message Toast::error('oops'); // Return a HTTP response to initiate the new session return Redirect::to('home');
使用方法链创建多个 toast
以下操作将向会话添加一个错误和 info toast 并重定向到 home。
// Create the message Toast::error('oops') ->info('hello'); // Return a HTTP response to initiate the new session return Redirect::to('home');
使用辅助函数发送带有标题的消息
以下操作将向会话添加一个 toast 并重定向到 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 进行修改。