A solid integration with the Laravel framework

Last updated at 08 May 2022 | Published at 28 November 2020

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


PHPFlasher offers a solid integration Laravel, with supports from Laravel 4.0 to 9.

Laravel

By default Laravel has a built-in flashing system. you can check it in the session section of the documentation.

Data stored in the session using this method will be available immediately and during the subsequent HTTP request. After the subsequent HTTP request, the flashed data will be deleted.

$request->session()->flash('success', 'Task was successful!');

And then somewhere in the view, you can use the session()->get('success') method to get the data.

@if (session()->has('success'))
    <div class="fixed bg-green-600 text-white py-2 px-4 rounded-xl bottom-3 right-3 text-sm">
        <p>{{ session()->get('success') }}</p>
    </div>
@endif

This example uses tailwindcss for styling, but you can use any CSS framework you want.

This approach is simple enough to add a flash message to your app. But :

  1. You can store only on flash type at a time. like success, error, info, warning
  2. You have to make changes to the view to use another CSS framework than tailwindcss like bootstrap or material-ui
  3. But what if you want to use multiple CSS frameworks for different types of flash messages ?
  4. How you can sort or limit the displayed flash messages ?
  5. How about translations and rtl languages ?
  6. Do you want to use some javascript library to display the flash messages ? (like sweetalert or toastr)

PHPFlasher take this approach of storing the flash messages in the session and then retrieving it in the view to the next level. And solve all the above issues in a simple and easy way.

Requirements

To use PHPFlasher in a Laravel application, you need :

PHP >= 5.3 Laravel >= 4.0


Installation

You can install the PHPFlasher Laravel package using composer.
This is the base package for all Laravel adapters.

composer require php-flasher/flasher-laravel

Usage

Dispatch notifications from anywhere in your application

namespace App\Http\Controllers;

use App\Post;
use App\Http\Requests\PostRequest;
use Flasher\Prime\FlasherInterface;

class PostController extends Controller
{
    public function store(PostRequest $request)
    {
        $post = Post::create($request->only(['title', 'body']));

        if ($post instanceof Post) {
            flash()->addSuccess('Data has been saved successfully!');
            // if you like to use Facades : Flasher::addSuccess('Data has been saved successfully!');

            return redirect()->route('posts.index');
        }

        flash()->addError('An error has occurred please try again later.');

        return back();
    }
}

For Laravel version < 5.5

in Laravel version 5.5 and beyond this step can be skipped if package auto-discovery is enabled.

Add the service provider to config/app.php.

'providers' => [
    ...
    Flasher\Laravel\FlasherServiceProvider::class,
    ...
];

Optionally include the Facade in config/app.php.

'Flasher' => Flasher\Laravel\Facade\Flasher::class,
Younes

PHPFlasher is a project by Younes KHOUBZA.