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


PHPFlasher is modular and consists of multiple libraries, allowing users to install and use only the specific components they need for their project.

Installation

PHPFlasher can be installed using composer :

Laravel:

composer require php-flasher/flasher-laravel


Symfony:

composer require php-flasher/flasher-symfony

PHPFlasher includes a default notification style , but users can also install additional adapters to customize the appearance of notifications within their projects such as :


General Usage

To display a notification message, you can either use the flash() helper method or obtain an instance of flasher from the service container. Then, before returning a view or redirecting, call the addSuccess() method and pass in the desired message to be displayed.

#/ PHPFlasher

class BookController
{
    public function saveBook()
    {
        // ...

        flash('Your account has been reactivated.');
        
        flash()->addSuccess('Your subscription has been cancelled.');
        
        flash()
            ->success('Your account has been re-activated.')
            ->flash();
            
        app('flasher')->addSuccess('Your account has been re-activated.');

        // ... redirect or render the view
    }
    
    /**
     * if you prefer to use dependency injection 
     */
    public function register(FlasherInterface $flasher)
    {
        // ...
        
        $flasher->addSuccess('Your form has been submitted.');
        
        // ... redirect or render the view
    }
}


It’s important to choose a message that is clear and concise, and that accurately reflects the outcome of the operation.
In this case, "Book has been created successfully!" is already a good message, but you may want to tailor it to fit the specific context and language of your application.

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

#/ usage addSuccess

flash()->addSuccess('Your account has been re-verified.');
#/ usage addError

flash()->addError('There was a problem processing your request.');
#/ usage addWarning

flash()->addWarning('Your account may not have been re-activated.');
#/ usage addInfo

flash()->addInfo('Your message has been received and is being processed.');

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('warning', 'Your product may not have been shipped.');
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',
    ])
    ->addWarning('Your account may not have been re-verified.');
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('An error occurred.');
param description
$option Option key
$value Option value

priority

Sets the priority of a flash message, the highest priority will be displayed first.

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

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

flash()
    ->priority(1)
    ->addError('Priority 1 → There was an issue updating your address.');

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

flash()
    ->priority(2)
    ->addInfo('Priority 2 → Your account has been reinstated and a confirmation email has been sent.');
param description
$priority The priority of the notification, the higher the priority, the sooner it will be displayed

hops

This method sets the number of requests that the flash message should persist for. By default, flash messages are only displayed for a single request and are then discarded. By setting the number of hops, the flash message will be persisted for multiple requests.

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)
    ->addWarning('Your message may not have been received.');
param description
$hops indicate how many requests the flash message will persist for

translate

This method sets the locale to be used for the translation of the flash message. If a non-null value is provided, the flash message will be translated into the specified language. If null is provided, the default locale will be used.

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

It is important to note that the translate() method only sets the locale to be used for the translation of the flash message. It does not actually perform the translation itself.

In order to translate the flash message, you will need to provide the appropriate translation keys in your translation files.

In the above example, to translate the flash message into Arabic, If you are using Laravel you will need to add the following keys to the resources/lang/ar/messages.php file:

return [
    'Your request was processed successfully.' => 'تمت العملية بنجاح.',
    'Congratulations!' => 'تهانينا',
];
Younes

PHPFlasher is a project by Younes KHOUBZA.