royvoetman/laravel-flash-alerts

Automatically flash success alerts in resources controllers: `store`, `update` and `destroy` methods.

v1.0.6 2023-06-17 07:36 UTC

This package is auto-updated.

Last update: 2024-10-17 10:15:47 UTC


README

This packages will automatically flash success messages to the session when a controller's: store, update or destroy method was successfully executed.

An execution is considered successful when no exceptions were raised and no messages are defined in the current session under the key warning.

Latest Version MIT Licensed Total Downloads

Installation

composer require royvoetman/laravel-flash-alerts

Add FlashAlerts middleware to the routeMiddleware array in app/Http/Kernel.php

 /**
  * The application's route middleware.
  *
  * These middleware may be assigned to groups or used individually.
  *
  * @var array
  */
 protected $routeMiddleware = [
    ...
    'flash.alerts' => \RoyVoetman\LaravelFlashAlerts\Middleware\FlashAlerts::class
 ];

Add the FlashesAlerts trait to your applications BaseController

<?php

namespace App\Http\Controllers;

use RoyVoetman\LaravelFlashAlerts\Traits\FlashesAlerts;
...

abstract class Controller extends BaseController
{
    use FlashesAlerts;

    ...
}

Signature

public function registerAlertMiddleware(string $model, array $except = []);
  • $model
    • A string used in the flash messages for example $model = 'Book' will result in: The Book has been successfully added
  • $except
    • An array of methods that are skipped while registering middleware.

Usage

If the middleware is registered and a store, update or destory method has been successfully executed. A message will be flashed in the current session under the key alert.

If the current session has a message under the key warning or an exception is thrown, the request will not be considered successful.

Example

<?php

namespace App\Http\Controllers;

class BookController extends Controller
{    
    /**
     * BookController constructor.
     */
    public function __construct()
    {
        parent::__construct();
        
        $this->registerAlertMiddleware('Book');
    }
    
    ...
}

Ignoring methods

<?php

namespace App\Http\Controllers;

class BookController extends Controller
{    
    /**
     * BookController constructor.
     */
    public function __construct()
    {
        parent::__construct();
        
        $this->registerAlertMiddleware('Book', ['destroy']);
    }
    
    public function store()
    {
        // Will flash alert

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

    public function destroy()
    {
        // Won't flash alert

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

Displaying flash alerts

@if (session()->has('alert'))
    <div class="alert alert-success" role="alert">
        {{ session('alert') }}
    </div>
@endif

Change Alert messages

php artisan vendor:publish --provider="RoyVoetman\LaravelFlashAlerts\FlashAlertsServiceProvider" 

This will place the overwritable translations under resources/lang/vendor/laravel-flash-alerts

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Contributions are welcome and will be fully credited. We accept contributions via Pull Requests on Github.

Pull Requests

  • PSR-2 Coding Standard - The easiest way to apply the conventions is to install PHP Code Sniffer.
  • Document any change in behaviour - Make sure the README.md and any other relevant documentation are kept up-to-date.
  • Create feature branches - Don't ask us to pull from your master branch.
  • One pull request per feature - If you want to do more than one thing, send multiple pull requests.

License

The MIT License (MIT). Please see License File for more information.