|
| 1 | +# Flash |
| 2 | + |
| 3 | +[]() |
| 4 | +[]() |
| 5 | +[](https://travis-ci.org/codezero-be/flash) |
| 6 | +[](https://scrutinizer-ci.com/g/codezero-be/flash) |
| 7 | +[](https://packagist.org/packages/codezero/flash) |
| 8 | + |
| 9 | +### Flash messages to the session with [Laravel 5](http://laravel.com/). |
| 10 | + |
| 11 | +This package is based on the original version of [Laracasts](https://github.com/laracasts/flash), but adds a few extra features: |
| 12 | + |
| 13 | +- Flash multiple messages in one request. |
| 14 | +- Pass in a simple message or a key to look up in [Laravel's Translator](http://laravel.com/docs/5.0/localization). |
| 15 | + |
| 16 | +## Laravel 5 Installation |
| 17 | + |
| 18 | +Install this package through Composer: |
| 19 | + |
| 20 | + composer require codezero/flash |
| 21 | + |
| 22 | +Add a reference to `FlashServiceProvider` to the providers array in `config/app.php`: |
| 23 | + |
| 24 | + 'providers' => [ |
| 25 | + 'CodeZero\Flash\FlashServiceProvider' |
| 26 | + ] |
| 27 | + |
| 28 | +If you want to use the facade, then you can also add it there: |
| 29 | + |
| 30 | + 'aliases' => [ |
| 31 | + 'Flash' => 'CodeZero\Flash\Facade\Flash' |
| 32 | + ] |
| 33 | + |
| 34 | +## Usage |
| 35 | + |
| 36 | +You can flash a message to the session in your controllers, before you redirect. |
| 37 | + |
| 38 | +### 3 Ways to Flash... |
| 39 | + |
| 40 | +You can use the facade... |
| 41 | + |
| 42 | + Flash::info('message'); |
| 43 | + |
| 44 | +... or the helper method... |
| 45 | + |
| 46 | + flash()->info('message'); |
| 47 | + |
| 48 | +... or you can inject `CodeZero\Flash\Flasher` in your controller and call it like this: |
| 49 | + |
| 50 | + $this->flash->info('message'); |
| 51 | + |
| 52 | +### Flash Types |
| 53 | + |
| 54 | + Flash::info('info message'); |
| 55 | + Flash::success('success message'); |
| 56 | + Flash::warning('warning message'); |
| 57 | + Flash::error('error message'); |
| 58 | + Flash::overlay('modal overlay message', 'Message Title'); |
| 59 | + |
| 60 | +### Multiple Flashes |
| 61 | + |
| 62 | +You can flash any number of messages of any type. |
| 63 | +In your view, you can use `@foreach` to run through them (see below). |
| 64 | + |
| 65 | + Flash::success('success message'); |
| 66 | + Flash::warning('warning message'); |
| 67 | + |
| 68 | +... or... |
| 69 | + |
| 70 | + Flash::success('success message')->warning('warning message'); |
| 71 | + |
| 72 | +### Dismissible Messages |
| 73 | + |
| 74 | +By default an `alert-dismissible` class is added and a close button is shown. |
| 75 | +If you don't want this, you can pass `false` as the third argument. |
| 76 | +Obviously an overlay message is always dismissible and does not have this option. |
| 77 | + |
| 78 | + Flash::success('success message', [], false); |
| 79 | + |
| 80 | +> Dismissing alerts requires some javascript from [Bootstrap](http://getbootstrap.com/getting-started/), or your own. |
| 81 | +
|
| 82 | +### Localization |
| 83 | + |
| 84 | +If you want to fetch a localized message from [Laravel's Translator](http://laravel.com/docs/5.0/localization), |
| 85 | +then you can pass in the translation key instead of a message. |
| 86 | +As a second parameter, you can optionally provide an array of placeholder values for the translated string. |
| 87 | + |
| 88 | + Flash::error('users.user_not_found', ['id' => $id]); |
| 89 | + Flash::overlay('users.user_not_found', 'users.title', ['id' => $id]); |
| 90 | + |
| 91 | +This would look for an array key `user_not_found` and `title` in `resources/lang/en/users.php` (if your locale is `en`) |
| 92 | +and replace `:id` with the actual `$id`. |
| 93 | + |
| 94 | + 'user_not_found' => 'There is no user with the ID of :id.' |
| 95 | + 'title' => 'User :id Not Found' |
| 96 | + |
| 97 | +### Show the Messages |
| 98 | + |
| 99 | +In your master view, simply include a partial: |
| 100 | + |
| 101 | + @include('flash::message') |
| 102 | + |
| 103 | +A full example with [Bootstrap](http://getbootstrap.com/): |
| 104 | + |
| 105 | + <!DOCTYPE html> |
| 106 | + <html lang="en"> |
| 107 | + <head> |
| 108 | + <meta charset="UTF-8"> |
| 109 | + <title>Document</title> |
| 110 | + <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css"> |
| 111 | + </head> |
| 112 | + <body> |
| 113 | + |
| 114 | + <div class="container"> |
| 115 | + |
| 116 | + @include('flash::message') |
| 117 | + |
| 118 | + <p>Welcome...</p> |
| 119 | + |
| 120 | + </div> |
| 121 | + |
| 122 | + <script src="//code.jquery.com/jquery.js"></script> |
| 123 | + <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script> |
| 124 | + |
| 125 | + <!-- This is only necessary if you do Flash::overlay('...') --> |
| 126 | + <script> |
| 127 | + $('#flash-overlay-modal').modal(); |
| 128 | + </script> |
| 129 | + |
| 130 | + </body> |
| 131 | + </html> |
| 132 | + |
| 133 | +### Customize the Messages |
| 134 | + |
| 135 | +If you want to customize the templates you can publish the views: |
| 136 | + |
| 137 | + php artisan vendor:publish --provider="CodeZero\Flash\FlashServiceProvider" --tag="views" |
| 138 | + |
| 139 | +> You will find the views in `resources/views/vendor/codezero/flash`. |
| 140 | +
|
| 141 | +If you want to change the class names or session key, publish the configuration file: |
| 142 | + |
| 143 | + php artisan vendor:publish --provider="CodeZero\Flash\FlashServiceProvider" --tag="config" |
| 144 | + |
| 145 | +> You will find the configuration file at `config/flash.php`. |
| 146 | +
|
| 147 | +## Testing |
| 148 | + |
| 149 | + $ vendor/bin/phpspec run |
| 150 | + |
| 151 | +## Security |
| 152 | + |
| 153 | +If you discover any security related issues, please [e-mail me](mailto:ivan@codezero.be) instead of using the issue tracker. |
| 154 | + |
| 155 | +## License |
| 156 | + |
| 157 | +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
| 158 | + |
| 159 | +--- |
| 160 | +[](https://github.com/igrigorik/ga-beacon) |
0 commit comments