Jalle19 / laravel-unshitty-flash by Jalle19

Highly opinionated flash message service for Laravel
355
1
2
Package Data
Maintainer Username: Jalle19
Maintainer Contact: neggelandia@gmail.com (Sam Stenvall)
Package Create Date: 2017-04-05
Package Last Update: 2017-04-05
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-11-22 03:04:15
Package Statistics
Total Downloads: 355
Monthly Downloads: 1
Daily Downloads: 0
Total Stars: 1
Total Watchers: 2
Total Forks: 0
Total Open Issues: 0

laravel-unshitty-flash

Build Status Scrutinizer Code Quality Coverage Status

Highly opinionated flash message service for Laravel. In contrast to practically all other similar libraries I've seen, this one:

  • Supports multiple messages
  • Supports multiple identical messages
  • Supports flashing immediately, i.e. to the current request
  • Let's you configure the session key used
  • Forces you to inject the service, no magic facade
  • Forces you to operate on the Request object itself

Installation

Install the package:

composer require jalle19/laravel-unshitty-flash

Register the service provider:

'providers' => [
	...
	Jalle19\Laravel\UnshittyFlash\FlashServiceProvider::class,
	...
]

Publish the configuration file:

php artisan vendor:publish --provider="Jalle19\Laravel\UnshittyFlash\FlashServiceProvider"

Usage

Inject FlashService into the controller you want to create flash messages from, then use it like this:

$this->flashService->success($request, 'Some successful message');
$this->flashService->info($request, 'Some informational message');
$this->flashService->warning($request, 'Some warning');
$this->flashService->danger($request, 'Some dangerous message');

If you need to flash a message to the current request, (e.g. from a middleware that displays a permanent message), pass true as the third parameter:

$this->flashService->info($request, 'Permanently visible message', true);

If the message levels above are not enough for you, you can use an arbitrary level using the message() method:

$this->flashService->message($request, 'Some rant about libraries', 'rant');

To render the flash messages in your views, you can use something like the following snippet:

@foreach (session()->get(config('flash.session_key'), []) as $notification)
    <div class="alert alert-{{ $notification['level'] }} alert-dismissible in">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>

        {!! $notification['message'] !!}
    </div>    
@endforeach