softon / sweetalert
Laravel 5 包用于 SweetAlert2。使用此包在 Laravel 应用中轻松显示 sweetalert2 提示。
v1.0.2
2020-04-17 06:43 UTC
Requires
- php: >=5.4.0
- guzzlehttp/guzzle: ~5.0|~6.0
- illuminate/support: ~5.6.0|~5.7.0|~5.8.0|^6.0|^7.0
This package is auto-updated.
Last update: 2024-09-27 16:53:33 UTC
README
Laravel 5 包用于 SweetAlert 2。使用此包在 Laravel 应用中轻松显示 SweetAlert 提示。
安装
- 使用 composer 安装此包
$ composer require softon/sweetalert
- (可选:Laravel 5.5) 将服务提供者添加到 Laravel 的 config/app.php 文件中
Softon\SweetAlert\SweetAlertServiceProvider::class,
- (可选:Laravel 5.5) 在 Laravel 的 config/app.php 文件中添加 Facade 的别名
'SWAL' => Softon\SweetAlert\Facades\SWAL::class,
- 运行以下命令以发布配置和视图
$ php artisan vendor:publish
视图
此包包含自己的视图,可包含在您的模板中。但如果您想修改它或包含自己的视图,可以使用 resources/views/vendor/sweetalert
目录中发布的视图。
此包还包括 SweetAlert2 CDN,如果您尚未从其网站包含 SweetAlert2 JavaScript 文件,则可以使用它。必须首先加载 CDN 视图。
对于内置视图,您可以在 blade 模板中关闭 body 标签之前使用以下内容
@include('sweetalert::cdn') // Optional needed only if SweetAlert2 files are not inserted by the developer @include('sweetalert::view') @include('sweetalert::validator') // Optional needed only to show form validation errors automatically
或对于已发布的视图,使用以下内容
@include('vendor.sweetalert.cdn') // Optional needed only if SweetAlert2 files are not inserted by the developer @include('vendor.sweetalert.view') @include('vendor.sweetalert.validator') // Optional needed only to show form validation errors automatically
配置
您可以通过参考 SweetAlert2 文档来更改包的基本参数,获取更多详细信息。
用法
您可以使用 SWAL Facade 或 swal 辅助函数来调用方法。
使用 SWAL Facade 向用户显示消息
use Softon\SweetAlert\Facades\SWAL; // Params: [Title,Text,Type,Options[]] SWAL::message('Good Job','You have successfully logged In!','info'); SWAL::message('Good Job','You have successfully logged In!','error'); SWAL::message('Good Job','You have successfully logged In!','success',['timer'=>2000]); // For All available options please refer the SweetAlert 2 Docs
使用 swal 辅助函数向用户显示消息
// Message Type Can be `warning`, `error`, `success`, `info` and `question`. Based on this there are some convinence function that can be used instead of the message method.: swal('Your Title','Text'); swal()->message('Good Job','You have successfully logged In!','info'); swal()->message('Good Job','You have successfully logged In!','error'); swal()->message('Good Job','You have successfully logged In!','success',['timer'=>2000]);
// Params [Title, Text, Options] swal()->warning('Good Job','You have successfully logged In!',[]); swal()->error('Good Job','You have successfully logged In!',[]); swal()->success('Good Job','You have successfully logged In!',[]); swal()->info('Good Job','You have successfully logged In!',[]); swal()->question('Good Job','You have successfully logged In!',[]);
显示将在几秒钟后自动关闭的模态框
swal()->autoclose(2000)->message('Good Job','You have successfully logged In!','info'); swal()->autoclose(5000)->success('Good Job','You have successfully logged In!');
显示将在几秒钟后自动关闭的 toast 模态框
swal()->toast()->autoclose(2000)->message('Good Job','You have successfully logged In!','info');
更改确认按钮文本
swal()->button('Close Me')->message('Good Job','You have successfully logged In!','info'); // Button Params [Button Text,Button Colour, SWAL Style Enable / Disable, Style Class for Buttons] swal()->button('Close Me','#efefef',false,'btn btn-primary')->info('Good Job','You have successfully logged In!');
更改模态框的位置
// Possible Positions : `top`, `top-left`, `top-right`, `center`, `center-left`, `center-right`, `bottom`, `bottom-left`, or `bottom-right` swal()->position('top')->message('Good Job','You have successfully logged In!','info');
您可以链式调用任何这些方法以组合功能
swal()->position('bottom-right')->autoclose(3000)->toast()->message('This is A Custom Message');