Blog Detail

19

Nov
A Powerful Flash notifications System for PHP & Laravel cover image

arrow_back A Powerful Flash notifications System for PHP & Laravel

Flash messages are a great way to add some simple notifications to users of your website or application about important events that may have happened. PHPFlasher gives a straightforward way to give feedback messages on the current or next page to users with the flashing system. The flashing system basically makes it possible to record a message and store it within the session so that it can be retrieved when needed.

Php Flasher

PHP Flasher helps you to add flash notifications to your PHP projects. This library was developed with the idea that you should be able to add flash notifications to your application with ease and with few lines of code. No application-wide rewrites and no big investments upfront.

Why use PHP Flasher?

PHPFlasher supports many notification libraries: tailwindcss, bootstrap, toastr.js, sweet alert 2, pnotify, noty, notyf, and even show desktop notifications.

This library is designed, so you can take full control when creating your notifications :

  • Show various notifications simultaneously
  • Sort and filter notifications
  • Render notifications from JSON response (ajax)
  • Limit the number of displayed notifications
  • Show Desktop notifications for Linux, macOS and Windows
  • Framework-agnostic with implementations for Laravel and Symfony
  • Autocomplete for phpstorm
  • You can always create an adapter yourself
  • …and more

Installation

PHPFlasher can be installed using composer.

For Php:
composer require php-flasher/flasher

PHPFlasher also offers solid integration with Laravel and Symfony :

For Laravel:
composer require php-flasher/flasher-laravel
For Symfony:
composer require php-flasher/flasher-symfony

Additionally, you may want to install an extra adapter (library) to display a specific type of notifications, you can find the adapters in the menu.

General Usage

If you’re using a framework like Laravel or Symfony, just grab an instance of FlasherInterface from the container

<?php

namespace App\Controller;

use Flasher\Prime\FlasherInterface;

class NotifyController
{
    public function flasher(FlasherInterface $flasher)
    {
        // ...

        $flasher->addSuccess('Data has been saved successfully!');

        flasher('Data has been saved successfully!') // only for the Laravel framework
        // ... redirect or render a view here
    }
}

There are also 4 shortcuts for the addFlash() method :

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

Notification Builder

There are only two main steps to render a notification : build and flash

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


// Step2 : Store the notification in the session
$builder->flash();
$builder = $flasher->type('error', 'An error has occurred please try again later.');
$builder->flash();

By following the above-given instructions you can learn the basic use of Php Flasher. But if you want to dig more you can visit its detailed documentation and View source code on Github.

Published at : 19-11-2021

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project