If you find PHPFlasher useful, please consider giving it a ⭐ star on GitHub 😊. If you spot any typos or have suggestions, feel free to contribute to the documentation 📝. Your feedback helps keep the project up-to-date and well-maintained. Thank you
Laravel
composer require php-flasher/flasher-sweetalert-laravel
After installation, you need to run another command to set up the necessary assets for PHPFlasher:
php artisan flasher:install
Note: The configuration settings below are the default ones. You only need to change them if you want to customize the default behavior.
<?php // config/flasher.php
return [
'plugins' => [
'sweetalert' => [
'scripts' => [
'/vendor/flasher/sweetalert2.min.js',
'/vendor/flasher/flasher-sweetalert.min.js',
],
'styles' => [
'/vendor/flasher/sweetalert2.min.css',
],
'options' => [
// Optional: Add global options here
// 'position' => 'center'
],
],
],
];
Symfony
composer require php-flasher/flasher-sweetalert-symfony
After installation, you need to run another command to set up the necessary assets for PHPFlasher:
php bin/console flasher:install
Note: The configuration settings below are the default ones. You only need to change them if you want to customize the default behavior.
# config/packages/flasher.yaml
flasher:
plugins:
sweetalert:
scripts:
- '/vendor/flasher/sweetalert2.min.js'
- '/vendor/flasher/flasher-sweetalert.min.js'
styles:
- '/vendor/flasher/sweetalert2.min.css'
options:
# Optional: Add global options here
# position: center
Usage
The methods described in the Usage section can also be used with the
notyf
adapter.
To display a notification message, you can either use the sweetalert()
helper method or obtain an instance of sweetalert
from the service container.
Then, before returning a view or redirecting, call the success()
method and pass in the desired message to be displayed.
#/ noty
use Flasher\SweetAlert\Prime\SweetAlertInterface;
class BookController
{
public function saveBook()
{
sweetalert()->success('Your account has been restored.');
// or simply
sweetalert('Your account has been restored.');
}
/**
* if you prefer to use dependency injection
*/
public function register(SweetAlertInterface $sweetalert)
{
// ...
$sweetalert->success('Your email has been verified.');
// ... redirect or render the view
}
}
#/ usage info
sweetalert()->info('Your subscription has been cancelled and a confirmation email has been sent.');
#/ usage warning
sweetalert()->warning('Your password may be at risk.');
#/ usage error
sweetalert()->error('There was an issue verifying your account.');
For more information on Sweetalert2 alert options and usage, please refer to the original documentation at https://sweetalert2.github.io
The methods described in the Usage section can also be used with the
notyf
adapter.
Add a customized icon for the popup. Should contain a string with the path or URL to the image.
sweetalert()->imageUrl(
string $imageUrl,
int $imageWidth = null,
int $imageHeight = null,
string $imageAlt = null
);
Popup window position, can be top
, top-start
, top-end
, center
, center-start
, center-end
, bottom
, bottom-start
or bottom-end
.
sweetalert()->position(string $position);
Whether or not an alert should be treated as a toast notification. This option is normally coupled with the position parameter and a timer. Toasts are NEVER autofocused.
sweetalert()->toast(bool $toast = true, string $position = 'top-end', bool $showConfirmButton = false);
Auto close timer of the popup. Set in ms (milliseconds).
sweetalert()->timer(int $timer);
If set to true
, the timer will have a progress bar at the bottom of a popup. Mostly, this feature is useful with toasts.
sweetalert()->timerProgressBar(bool $timerProgressBar = true);
Whether or not SweetAlert2 should show a full screen click-to-dismiss backdrop. Can be either a boolean or a string which will be assigned to the CSS background property.
sweetalert()->backdrop(bool $backdrop = true);
Paired with window position, sets the direction the popup should grow in, can be set to row
, column
, fullscreen
or false
.
sweetalert()->grow(bool|string $grow);
If set to false
, a Confirm
button will not be shown.
sweetalert()->showConfirmButton(
bool $showConfirmButton = true,
string $confirmButtonText = null,
string $confirmButtonColor = null,
string $confirmButtonAriaLabel = null
);
If set to true
, a Deny
button will be shown. It can be useful when you want a popup with 3 buttons.
sweetalert()->showDenyButton(
bool $showDenyButton = true,
string $denyButtonText = null,
string $denyButtonColor = null,
string $denyButtonAriaLabel = null
);
If set to true
, a Cancel
button will be shown, which the user can click on to dismiss the modal.
sweetalert()->showCancelButton(
bool $showCancelButton = true,
string $cancelButtonText = null,
string $cancelButtonColor = null,
string $cancelButtonAriaLabel = null
);
Use this to change the text on the Confirm
button.
sweetalert()->confirmButtonText(
string $confirmButtonText,
string $confirmButtonColor = null,
string $confirmButtonAriaLabel = null
);
Use this to change the text on the Deny
button.
sweetalert()->denyButtonText(
string $denyButtonText,
string $denyButtonColor = null,
string $denyButtonAriaLabel = null
);
Use this to change the text on the Cancel
button.
sweetalert()->cancelButtonText(
string $cancelButtonText,
string $cancelButtonColor = null,
string $cancelButtonAriaLabel = null
);
Set to true
to show close button in top right corner of the popup.
sweetalert()->showCloseButton(bool $showCloseButton = true);