|
| 1 | +# Flash messages made easy. Based on laracasts/flash |
| 2 | + |
| 3 | +## Installation |
| 4 | + |
| 5 | +First, pull in the package through Composer. |
| 6 | + |
| 7 | +Run `composer require mtxr/laravel-flash-message` |
| 8 | + |
| 9 | +And then, if using Laravel 5, include the service provider within `config/app.php`. |
| 10 | + |
| 11 | +```php |
| 12 | +'providers' => [ |
| 13 | + FlashMessage\FlashServiceProvider::class, |
| 14 | +]; |
| 15 | +``` |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +Within your controllers, before you perform a redirect... |
| 20 | + |
| 21 | +```php |
| 22 | +public function store() |
| 23 | +{ |
| 24 | + flash('Welcome Aboard!'); |
| 25 | + |
| 26 | + return home(); |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +You may also do: |
| 31 | + |
| 32 | +- `flash('Message', 'info')` |
| 33 | +- `flash('Message', 'success')` |
| 34 | +- `flash('Message', 'danger')` |
| 35 | +- `flash('Message', 'warning')` |
| 36 | +- `flash('Message')->important()` |
| 37 | + |
| 38 | +If you need, you can flash two messages in the same request: |
| 39 | + |
| 40 | +```php |
| 41 | +public function welcome() |
| 42 | +{ |
| 43 | + flash('Welcome Aboard!', 'success'); |
| 44 | + |
| 45 | + flash('Request Failed!', 'error'); |
| 46 | + |
| 47 | + return home(); |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +Behind the scenes, this will set a few keys in the session: |
| 52 | + |
| 53 | +- 'flash_notification.messages' - The array of messages you have |
| 54 | + |
| 55 | +With this message flashed to the session, you may now display it in your view(s). Maybe something like: |
| 56 | + |
| 57 | +```html |
| 58 | +@if (session()->has('flash_notification.messages')) |
| 59 | + @foreach(session('flash_notification.messages') as $messageData) |
| 60 | + <div class="alert alert-{{ $messageData['level'] }} {{ $messageData['important'] ? 'alert-important' : '' }}"> |
| 61 | + @if(!$messageData['important']) |
| 62 | + <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> |
| 63 | + @endif |
| 64 | + |
| 65 | + {!! trans($messageData['message']) !!} |
| 66 | + </div> |
| 67 | + @endforeach |
| 68 | +@endif |
| 69 | +``` |
| 70 | + |
| 71 | +> Note that this package is optimized for use with Twitter Bootstrap. |
| 72 | +
|
| 73 | +Because flash messages and overlays are so common, if you want, you may use (or modify) the views that are included with this package. Simply append to your layout view: |
| 74 | + |
| 75 | +```html |
| 76 | +@include('flash::message') |
| 77 | +``` |
| 78 | + |
| 79 | +## Example |
| 80 | + |
| 81 | +```html |
| 82 | +<!DOCTYPE html> |
| 83 | +<html lang="en"> |
| 84 | +<head> |
| 85 | + <meta charset="UTF-8"> |
| 86 | + <title>Document</title> |
| 87 | + <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> |
| 88 | +</head> |
| 89 | +<body> |
| 90 | + |
| 91 | +<div class="container"> |
| 92 | + @include('flash::message') |
| 93 | + |
| 94 | + <p>Welcome to my website...</p> |
| 95 | +</div> |
| 96 | +</body> |
| 97 | +</html> |
| 98 | +``` |
| 99 | + |
| 100 | +If you need to modify the flash message partials, you can run: |
| 101 | + |
| 102 | +```bash |
| 103 | +php artisan vendor:publish |
| 104 | +``` |
| 105 | + |
| 106 | +The package view will now be located in the `app/views/packages/mtxr/laravel-flash-message/` directory. |
| 107 | + |
| 108 | +## Hiding Flash Messages |
| 109 | + |
| 110 | +A common desire is to display a flash message for a few seconds, and then hide it. To handle this, write a simple bit of JavaScript. For example, using jQuery, you might add the following snippet just before the closing `</body>` tag. |
| 111 | + |
| 112 | +``` |
| 113 | +<script> |
| 114 | +$('.flash-message.alert').not('.alert-important').delay(3000).fadeOut(350); |
| 115 | +</script> |
| 116 | +``` |
| 117 | + |
| 118 | +This will find any alerts - excluding the important ones, which should remain until manually closed by the user - wait three seconds, and then fade them out. |
0 commit comments