nrz / sweet-alert
一个简单的PHP包,用于在Laravel框架中显示Sweet Alerts
Requires
- php: >=7.0
- illuminate/session: ~5.0|^6.0|^7.0|^8.0
- illuminate/support: ~5.0|^6.0|^7.0|^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- mockery/mockery: ^1.0
- phpunit/phpunit: ^7.0
- dev-master
- v2.x-dev
- 2.0.5
- v2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.4.3
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.1
- 1.3.0
- 1.2.0
- 1.1.6
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.2
- 1.0.1
- 1.0.0
- dev-uxweb-change-readme-about-facade
- dev-add-license-1
- dev-code-improvements
- dev-refactoring-for-sweetalert2-js-library
- dev-hotfix-persistent-alert-button
- dev-analysis-qg9w2Q
- dev-L42
This package is not auto-updated.
Last update: 2024-09-21 00:30:39 UTC
README
安装
使用Composer安装此包。
composer require uxweb/sweet-alert
如果使用laravel < 5.5,请在config/app.php
中包含服务提供者和别名。
'providers' => [ UxWeb\SweetAlert\SweetAlertServiceProvider::class, ]; 'aliases' => [ 'Alert' => UxWeb\SweetAlert\SweetAlert::class, ];
安装前端依赖
此包仅通过使用JavaScript "ALERT"的美丽替代品来工作。
使用CDN
<!DOCTYPE html> <html lang="en"> <head> <!-- Include this in your blade layout --> <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script> </head> <body> @include('sweet::alert') </body> </html>
使用Laravel Mix
使用Yarn安装
yarn add sweetalert --dev
使用NPM安装
npm install sweetalert --save-dev
在你的resources/js/bootstrap.js
文件中要求sweetalert。
// ... require("sweetalert"); // ...
然后确保在你的blade布局中包含你的脚本。如果你的script标签包含defer
属性,请移除它,因为defer
将延迟脚本的执行,这会导致错误,因为浏览器首先将sweet::alert
blade模板作为HTML渲染。
<!DOCTYPE html> <html lang="en"> <head> <!-- Scripts --> <script src="{{ asset('js/app.js') }}"></script> </head> <body> @include('sweet::alert') </body> </html>
最后使用Mix编译你的资产
npm run dev
使用方法
使用外观
首先在你的控制器中导入SweetAlert外观。
use SweetAlert;
在你的控制器中,在你执行重定向之前...
public function store() { SweetAlert::message('Robots are working!'); return Redirect::home(); }
以下是一些如何使用外观的示例
SweetAlert::message('Message', 'Optional Title'); SweetAlert::basic('Basic Message', 'Mandatory Title'); SweetAlert::info('Info Message', 'Optional Title'); SweetAlert::success('Success Message', 'Optional Title'); SweetAlert::error('Error Message', 'Optional Title'); SweetAlert::warning('Warning Message', 'Optional Title');
使用辅助函数
alert($message = null, $title = '')
除了前面列出的方法外,您还可以直接使用辅助函数而不指定任何消息类型。这样做与
alert()->message('Message', 'Optional Title')
类似,我们可以使用外观以相同的方法使用辅助函数
alert()->message('Message', 'Optional Title'); alert()->basic('Basic Message', 'Mandatory Title'); alert()->info('Info Message', 'Optional Title'); alert()->success('Success Message', 'Optional Title'); alert()->error('Error Message', 'Optional Title'); alert()->warning('Warning Message', 'Optional Title'); alert()->basic('Basic Message', 'Mandatory Title')->autoclose(3500); alert()->error('Error Message', 'Optional Title')->persistent('Close');
在你的控制器中,在你执行重定向之前...
/** * Destroy the user's session (logout). * * @return Response */ public function destroy() { Auth::logout(); alert()->success('You have been logged out.', 'Good bye!'); return home(); }
对于一般信息警报,只需这样做:alert('Some message');
(等同于alert()->message('Some message');
)。
使用中间件
中间件组
首先通过将中间件类UxWeb\SweetAlert\ConvertMessagesIntoSweetAlert::class
添加到你的app/Http/Kernel.php类的$middlewareGroups中,在web中间件组中注册中间件。
protected $middlewareGroups = [ 'web' => [ \App\Http\Middleware\EncryptCookies::class, ... \UxWeb\SweetAlert\ConvertMessagesIntoSweetAlert::class, ], 'api' => [ 'throttle:60,1', ], ];
确保你只将中间件注册在'web'组中。
路由中间件
或者,如果你只想将中间件分配给特定的路由,你应该将中间件添加到app/Http/Kernel.php
文件中的$routeMiddleware
。
protected $routeMiddleware = [ 'auth' => \App\Http\Middleware\Authenticate::class, .... 'sweetalert' => \UxWeb\SweetAlert\ConvertMessagesIntoSweetAlert::class, ];
下一步:在你的控制器中,设置你的返回消息(使用with()
)和发送适当的消息和类型。
return redirect('dashboard')->with('success', 'Profile updated!');
或
return redirect()->back()->with('error', 'Profile updated!');
注意:使用中间件时,如果检测到以下键闪入会话:
error
、success
、warning
、info
、message
、basic
,将显示警报。
最终考虑
默认情况下,所有警报将在合理的默认秒数后消失。
但不用担心,如果您需要指定不同的时间,您可以
// -> Remember!, the number is set in milliseconds alert('Hello World!')->autoclose(3000);
此外,如果您需要使警报在页面上持续存在,直到用户通过按确认按钮将其关闭
// -> The text will appear in the button alert('Hello World!')->persistent("Close this");
您可以使用html()方法在消息中渲染HTML,如下所示
// -> html will be evaluated alert('<a href="#">Click me</a>')->html()->persistent("No, thanks");
定制
配置
如果您需要为此包自定义默认配置选项,请导出配置文件
php artisan vendor:publish --provider "UxWeb\SweetAlert\SweetAlertServiceProvider" --tag=config
将sweet-alert.php
配置文件发布到您的config
目录。到目前为止,可以更改的唯一配置是所有自动关闭警报的计时器。
视图
如果您需要自定义包含的警报消息视图,请运行
php artisan vendor:publish --provider "UxWeb\SweetAlert\SweetAlertServiceProvider" --tag=views
包视图位于resources/views/vendor/sweet/
目录。
您可以自定义此视图以满足您的需求。
配置选项
您有权访问以下配置选项以构建自定义视图
Session::get('sweet_alert.text') Session::get('sweet_alert.title') Session::get('sweet_alert.icon') Session::get('sweet_alert.closeOnClickOutside') Session::get('sweet_alert.buttons') Session::get('sweet_alert.timer')
请检查网站上的“配置”部分以获取所有其他可用选项。
默认视图
sweet_alert.alert
会话键包含一个JSON配置对象,可以直接传递给Sweet Alert。
@if (Session::has('sweet_alert.alert')) <script> swal({!! Session::get('sweet_alert.alert') !!}); </script> @endif
请注意,{!! !!}
用于输出未转义的JSON配置对象,它不适用于{{ }}
转义输出标签。
自定义视图
这是一个示例,说明您如何自定义视图以满足您的需求
@if (Session::has('sweet_alert.alert')) <script> swal({ text: "{!! Session::get('sweet_alert.text') !!}", title: "{!! Session::get('sweet_alert.title') !!}", timer: {!! Session::get('sweet_alert.timer') !!}, icon: "{!! Session::get('sweet_alert.type') !!}", buttons: "{!! Session::get('sweet_alert.buttons') !!}", // more options }); </script> @endif
请注意,除了计时器选项外,您必须使用""
(双引号)包裹值。
测试
要运行包含的测试套件
vendor/bin/phpunit
演示
SweetAlert::message('Welcome back!'); return Redirect::home();
SweetAlert::message('Your profile is up to date', 'Wonderful!'); return Redirect::home();
SweetAlert::message('Thanks for comment!')->persistent('Close'); return Redirect::home();
SweetAlert::info('Email was sent!'); return Redirect::home();
SweetAlert::error('Something went wrong', 'Oops!'); return Redirect::home();
SweetAlert::success('Good job!'); return Redirect::home();
SweetAlert::info('Random lorempixel.com : <img src="http://lorempixel.com/150/150/">')->html(); return Redirect::home();
SweetAlert::success('Good job!')->persistent("Close"); return Redirect::home();
许可证
Sweet Alert for Laravel 是开源软件,使用 MIT 许可证授权,许可证详情请见此处。