If you find PHPFlasher useful, we would greatly appreciate your support in the form of a star rating ⭐ on GitHub or by sharing the project on Twitter click here. Your feedback helps us keep the package up-to-date and well-maintained. Thank you


General Usage

Using this package is actually pretty easy. Adding notifications to your application actually require only one line of code.

#/ usage addSuccess

flash()->addSuccess('Your donation has been received.');
#/ usage addError

flash()->addError('There was a problem re-activating your account.');
#/ usage addWarning

flash()->addWarning('Your donation may not have been received.');
#/ usage addInfo

flash()->addInfo('Your account has been terminated and a confirmation email has been sent.');

These four methods (addSuccess, addError, addWarning, addInfo) are simply convenience shortcuts for the addFlash method, allowing you to specify the type and message in a single method call rather than having to pass both as separate arguments to the addFlash method.

flash()->addFlash(string $type, string $message, string $title = null, array $options = [])
#/ usage addFlash

flash()->addFlash('info', 'Your account has been created, but requires verification.');
param description  
$type Notification type : success, error, warning, info ….etc  
$message The body of the message you want to deliver to your user. This may contain HTML. If you add links, be sure to add the appropriate classes for the framework you are using.  
$title The notification title, Can also include HTML  
$options Custom options for javascript libraries (toastr, noty, notyf …etc)  

Modifiers

options

You can specify custom options for the flash messages when using a JavaScript library like toastr, noty, or notyf.

The options() method allows you to set multiple options at once by passing an array of key-value pairs, while the option() method allows you to set a single option by specifying its name and value as separate arguments.

The optional $merge argument for the options() method can be used to specify whether the new options should be merged with any existing options, or whether they should overwrite them.

flash()->options(array $options, bool $merge = true);

Refer to the documentation for your chosen JavaScript library to see which options are available and how they should be formatted.

#/ usage options

flash()
    ->options([
        'timeout' => 3000, // 3 seconds
        'position' => 'top-center',
    ])
    ->addInfo('This may take some time. Do not refresh the page.');
param description
$options Custom options to be passed to the javascript libraries (toastr, noty, notyf …etc)
$merge Merge options if you call the options method multiple times

option

Set a single option by specifying its name and value as separate arguments.

flash()->option(string $option, mixed $value);
#/ usage option

flash()
    ->option('position', 'top-center')
    ->option('timeout', 3000)
    ->addError('There was an issue de-registering your account.');
param description
$option Option key
$value Option value

priority

flash()->priority(int $priority);
#/ usage priority

flash()
    ->priority(3)
    ->addSuccess('Priority 3 → Your file has been uploaded.');

flash()
    ->priority(1)
    ->addError('Priority 1 → There was a problem cancelling your subscription.');

flash()
    ->priority(4)
    ->addWarning('Priority 4 → Your review may not have been submitted.');

flash()
    ->priority(2)
    ->addInfo('Priority 2 → Your order has been shipped and a tracking number has been sent to your email.');
param description
$priority The priority of the notification, the higher the priority, the sooner it will be displayed

hops

Sometimes you may want a flash message to persist for longer than a single request. As an example, with a multi-page form, you may want to store messages until all pages have been filled.

flash()->hops(int $hops);
flash()
    ->hops(2)
    ->addSuccess('Your account has been restored.');
param description
$hops indicate how many requests the flash message will persist for

keep

flash()->keep();
flash()
    ->keep()
    ->addInfo('Your subscription has been cancelled and a confirmation email has been sent.');
description
Keep the notification in the next session requests

delay

flash()->delay(int $delay);
flash()
    ->delay(2)
    ->addError('There was a problem receiving your message.');
param description
$delay The number of requests in which the message will be waiting before being displayed

now

flash()->now();
flash()
    ->now()
    ->addWarning('Your account may not have been linked.');
description
Shortcut for flash()->delay(0)

translate

flash()->translate(string $locale = null);
#/ usage translate

flash()
    ->translate('ar')
    ->addSuccess('Your request was processed successfully.', 'Congratulations!');
#/ usage translate with position

flash()
    ->translate('ar')
    ->option('position', 'top-left')
    ->addSuccess('Your request was processed successfully.', 'Congratulations!');
param description
$locale The locale to be used for the translation or null to use the default locale

preset

$builder = flash()->preset(string $preset, bool $flash = true);
param description
$preset The preset to be used for the notification
$flash If true, the notification will be flashed after the preset is applied

context

$builder = flash()->context(array $context = []);
param description
$context Custom data to be available in javascript side

withStamp

$builder = flash()->withStamp(StampInterface $stamp);
param description
$stamp Attach a stamp to the current notification

with

$builder = flash()->with(array $stamps);
param description
$stamps Attach multiple stamps at the same time to the current notification

handler

$builder = flash()->handler(string $handler);
param description
$handler The handler name it will be used to choose to correct js adapter to be used to display the notification

getEnvelope

$envelope = flash()->getEnvelope();
param description
$envelope Get the current notification with all stamps and options attached to it

flash

Call the flash method at the end when your notification is ready to be displayed

$envelope = flash()->flash(array $stamps = []);
param description
$stamps Attach multiple stamps at the same time to the current notification
$envelope The current notification with all stamps and options attached to it
Younes

PHPFlasher is a project by Younes KHOUBZA.