If you like PHPFlasher please consider giving it a on github or by spreading the word on twitter. Your support allows me to keep this package up-to-date and maintainable. Thank you


General Usage

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

$flasher->addFlash('success', 'Data has been saved successfully!');
$notification = $flasher->addFlash(string $type, string $message, string $title = null, array $options = [])
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)  

To make life even easier, there are four helper methods for different notification types of notification. So instead of manually supplying the notification type, you can simply call the type as its method name.

$flasher->addSuccess('success message');
$flasher->addError('error message');
$flasher->addWarning('warning message');
$flasher->addInfo('info message');

Fluent Builder methods

There are only two main steps to display a notification using a fluent chainable way : build and flash.

// Step 1: create your notification and add options
$builder = $flasher->handler('toastr') // the handle() method here is optional
    ->type('success')
    ->message('your custom message')
    ->priority(2)
    ->option('timer', 5000);

// Step2 : Store the notification in the session
$builder->flash();

Its getting even better right ? it’s so easy to read and still very powerful. You can omit properties that you do not care about. and only set the ones that important to you.

$builder = $flasher->type('error', 'An error has occurred please try again later.');
$builder->flash();

as you can see, you should manually flash the notification, flasher has to know when your notification is ready to be displayed.


type

$builder = $flasher->type(string $type, string $message = null, string $title = null, array $options = [])
param description
$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
$options Custom options for javascript libraries (toastr, noty, notyf …etc)
$builder An instance of the notification builder, so you could chain other builder methods on it

type shortcuts

$builder = $flasher->success(string $message = null, string $title = null, array $options = [])
$builder = $flasher->error(string $message = null, string $title = null, array $options = [])
$builder = $flasher->warning(string $message = null, string $title = null, array $options = [])
$builder = $flasher->info(string $message = null, string $title = null, array $options = [])
description
Shortcuts for the $flasher->type() method

message

$builder = $flasher->message(string $message);
param description
$message The message to be displayed

title

$builder = $flasher->title(string $title);
param description
$title The notification title

options

$builder = $flasher->options(array $options, bool $merge = true);
param description
$options Custom options for javascript libraries (toastr, noty, notyf …etc)
$merge Merge options if you call the options method multiple times

option

$builder = $flasher->option(string $option, mixed $value);
param description
$option Option key
$value Option value

priority

$builder = $flasher->priority(int $priority);
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.

$builder = $flasher->hops(int $hops);
param description
$hops indicate how many requests the flash message will persist for

keep

$builder = $flasher->keep();
description
Keep the notification in the next session requests

delay

$builder = $flasher->delay(int $delay);
param description
$delay The number of requests in which the message will be waiting before being displayed

now

$builder = $flasher->now();
description
Shortcut for $flasher->delay(0)

translate

$builder = $flasher->translate(string $locale = null);
param description
$locale The locale to be used for the translation or null to use the default locale

preset

$builder = $flasher->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 = $flasher->context(array $context = []);
param description
$context Custom data to be available in javascript side

withStamp

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

with

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

handler

$builder = $flasher->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 = $flasher->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 = $flasher->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.