├── composer.json ├── phpunit.xml ├── readme.md └── src ├── Laracasts └── Flash │ ├── Flash.php │ ├── FlashNotifier.php │ ├── FlashServiceProvider.php │ ├── LaravelSessionStore.php │ ├── Message.php │ ├── OverlayMessage.php │ ├── SessionStore.php │ └── functions.php └── views ├── message.blade.php └── modal.blade.php /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "laracasts/flash", 3 | "description": "Easy flash notifications", 4 | "license": "MIT", 5 | "authors": [ 6 | { 7 | "name": "Jeffrey Way", 8 | "email": "jeffrey@laracasts.com" 9 | } 10 | ], 11 | "require": { 12 | "php": ">=5.4.0", 13 | "illuminate/support": "~5.0|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0|^12.0" 14 | }, 15 | "require-dev": { 16 | "mockery/mockery": "dev-master", 17 | "phpunit/phpunit": "^6.1|^9.5.10|^10.5" 18 | }, 19 | "autoload": { 20 | "psr-0": { 21 | "Laracasts\\Flash": "src/" 22 | }, 23 | "files": [ 24 | "src/Laracasts/Flash/functions.php" 25 | ] 26 | }, 27 | "minimum-stability": "stable", 28 | "extra": { 29 | "laravel": { 30 | "providers": [ 31 | "Laracasts\\Flash\\FlashServiceProvider" 32 | ], 33 | "aliases": { 34 | "Flash": "Laracasts\\Flash\\Flash" 35 | } 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | ./tests/ 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Easy Flash Messages for Your Laravel App 2 | 3 | This composer package offers a Twitter Bootstrap optimized flash messaging setup for your Laravel applications. 4 | 5 | ## Installation 6 | 7 | Begin by pulling in the package through Composer. 8 | 9 | ```bash 10 | composer require laracasts/flash 11 | ``` 12 | 13 | Next, as noted above, the default CSS classes for your flash message are optimized for Twitter Bootstrap. As such, either pull in the Bootstrap's CSS within your HTML or layout file, or write your own CSS based on these classes. 14 | 15 | ```html 16 | 17 | ``` 18 | 19 | ## Usage 20 | 21 | Within your controllers, before you perform a redirect, make a call to the `flash()` function. 22 | 23 | ```php 24 | public function store() 25 | { 26 | flash('Welcome Aboard!'); 27 | 28 | return home(); 29 | } 30 | ``` 31 | 32 | You may also do: 33 | 34 | - `flash('Message')->success()`: Set the flash theme to "success". 35 | - `flash('Message')->error()`: Set the flash theme to "danger". 36 | - `flash('Message')->warning()`: Set the flash theme to "warning". 37 | - `flash('Message')->overlay()`: Render the message as an overlay. 38 | - `flash()->overlay('Modal Message', 'Modal Title')`: Display a modal overlay with a title. 39 | - `flash('Message')->important()`: Add a close button to the flash message. 40 | - `flash('Message')->error()->important()`: Render a "danger" flash message that must be dismissed. 41 | 42 | With this message flashed to the session, you may now display it in your view(s). Because flash messages and overlays are so common, we provide a template out of the box to get you started. You're free to use - and even modify to your needs - this template how you see fit. 43 | 44 | ```html 45 | @include('flash::message') 46 | ``` 47 | 48 | ## Example 49 | 50 | ```html 51 | 52 | 53 | 54 | 55 | Document 56 | 57 | 58 | 59 | 60 |
61 | @include('flash::message') 62 | 63 |

Welcome to my website...

64 |
65 | 66 | 67 | 68 | 69 | 70 | 73 | 74 | 75 | 76 | ``` 77 | 78 | If you need to modify the flash message partials, you can run: 79 | 80 | ```bash 81 | php artisan vendor:publish --provider="Laracasts\Flash\FlashServiceProvider" 82 | ``` 83 | 84 | The two package views will now be located in the `resources/views/vendor/flash/` directory. 85 | 86 | ```php 87 | flash('Welcome Aboard!'); 88 | 89 | return home(); 90 | ``` 91 | 92 | ```php 93 | flash('Sorry! Please try again.')->error(); 94 | 95 | return home(); 96 | ``` 97 | 98 | ```php 99 | flash()->overlay('You are now a Laracasts member!', 'Yay'); 100 | 101 | return home(); 102 | ``` 103 | 104 | > [Learn exactly how to build this very package on Laracasts!](https://laracasts.com/lessons/flexible-flash-messages) 105 | 106 | ## Hiding Flash Messages 107 | 108 | 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 `` tag. 109 | 110 | ```html 111 | 114 | ``` 115 | 116 | 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. 117 | 118 | ## Multiple Flash Messages 119 | 120 | Need to flash multiple flash messages to the session? No problem. 121 | 122 | ```php 123 | flash('Message 1'); 124 | flash('Message 2')->important(); 125 | 126 | return redirect('somewhere'); 127 | ``` 128 | 129 | Done! You'll now see two flash messages upon redirect. 130 | 131 | 132 | -------------------------------------------------------------------------------- /src/Laracasts/Flash/Flash.php: -------------------------------------------------------------------------------- 1 | session = $session; 33 | $this->messages = collect(); 34 | } 35 | 36 | /** 37 | * Flash an information message. 38 | * 39 | * @param string|null $message 40 | * @return $this 41 | */ 42 | public function info($message = null) 43 | { 44 | return $this->message($message, 'info'); 45 | } 46 | 47 | /** 48 | * Flash a success message. 49 | * 50 | * @param string|null $message 51 | * @return $this 52 | */ 53 | public function success($message = null) 54 | { 55 | return $this->message($message, 'success'); 56 | } 57 | 58 | /** 59 | * Flash an error message. 60 | * 61 | * @param string|null $message 62 | * @return $this 63 | */ 64 | public function error($message = null) 65 | { 66 | return $this->message($message, 'danger'); 67 | } 68 | 69 | /** 70 | * Flash a warning message. 71 | * 72 | * @param string|null $message 73 | * @return $this 74 | */ 75 | public function warning($message = null) 76 | { 77 | return $this->message($message, 'warning'); 78 | } 79 | 80 | /** 81 | * Flash a general message. 82 | * 83 | * @param string|null $message 84 | * @param string|null $level 85 | * @return $this 86 | */ 87 | public function message($message = null, $level = null) 88 | { 89 | // If no message was provided, we should update 90 | // the most recently added message. 91 | if (! $message) { 92 | return $this->updateLastMessage(compact('level')); 93 | } 94 | 95 | if (! $message instanceof Message) { 96 | $message = new Message(compact('message', 'level')); 97 | } 98 | 99 | $this->messages->push($message); 100 | 101 | return $this->flash(); 102 | } 103 | 104 | /** 105 | * Modify the most recently added message. 106 | * 107 | * @param array $overrides 108 | * @return $this 109 | */ 110 | protected function updateLastMessage($overrides = []) 111 | { 112 | $this->messages->last()->update($overrides); 113 | 114 | return $this; 115 | } 116 | 117 | /** 118 | * Flash an overlay modal. 119 | * 120 | * @param string|null $message 121 | * @param string $title 122 | * @return $this 123 | */ 124 | public function overlay($message = null, $title = 'Notice') 125 | { 126 | if (! $message) { 127 | return $this->updateLastMessage(['title' => $title, 'overlay' => true]); 128 | } 129 | 130 | return $this->message( 131 | new OverlayMessage(compact('title', 'message')) 132 | ); 133 | } 134 | 135 | /** 136 | * Add an "important" flash to the session. 137 | * 138 | * @return $this 139 | */ 140 | public function important() 141 | { 142 | return $this->updateLastMessage(['important' => true]); 143 | } 144 | 145 | /** 146 | * Clear all registered messages. 147 | * 148 | * @return $this 149 | */ 150 | public function clear() 151 | { 152 | $this->messages = collect(); 153 | 154 | return $this; 155 | } 156 | 157 | /** 158 | * Flash all messages to the session. 159 | */ 160 | protected function flash() 161 | { 162 | $this->session->flash('flash_notification', $this->messages); 163 | 164 | return $this; 165 | } 166 | } 167 | 168 | -------------------------------------------------------------------------------- /src/Laracasts/Flash/FlashServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->bind( 24 | 'Laracasts\Flash\SessionStore', 25 | 'Laracasts\Flash\LaravelSessionStore' 26 | ); 27 | 28 | $this->app->singleton('flash', function () { 29 | return $this->app->make('Laracasts\Flash\FlashNotifier'); 30 | }); 31 | } 32 | 33 | /** 34 | * Bootstrap the application events. 35 | * 36 | * @return void 37 | */ 38 | public function boot() 39 | { 40 | $this->loadViewsFrom(__DIR__ . '/../../views', 'flash'); 41 | 42 | $this->publishes([ 43 | __DIR__ . '/../../views' => base_path('resources/views/vendor/flash') 44 | ]); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/Laracasts/Flash/LaravelSessionStore.php: -------------------------------------------------------------------------------- 1 | session = $session; 22 | } 23 | 24 | /** 25 | * Flash a message to the session. 26 | * 27 | * @param string $name 28 | * @param array $data 29 | */ 30 | public function flash($name, $data) 31 | { 32 | $this->session->flash($name, $data); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Laracasts/Flash/Message.php: -------------------------------------------------------------------------------- 1 | update($attributes); 50 | } 51 | 52 | /** 53 | * Update the attributes. 54 | * 55 | * @param array $attributes 56 | * @return $this 57 | */ 58 | public function update($attributes = []) 59 | { 60 | $attributes = array_filter($attributes); 61 | 62 | foreach ($attributes as $key => $attribute) { 63 | $this->$key = $attribute; 64 | } 65 | 66 | return $this; 67 | } 68 | 69 | 70 | /** 71 | * Whether the given offset exists. 72 | * 73 | * @param mixed $offset 74 | * @return bool 75 | */ 76 | #[\ReturnTypeWillChange] 77 | public function offsetExists($offset) 78 | { 79 | return isset($this->$offset); 80 | } 81 | 82 | /** 83 | * Fetch the offset. 84 | * 85 | * @param mixed $offset 86 | * @return mixed 87 | */ 88 | #[\ReturnTypeWillChange] 89 | public function offsetGet($offset) 90 | { 91 | return $this->$offset; 92 | } 93 | 94 | /** 95 | * Assign the offset. 96 | * 97 | * @param mixed $offset 98 | * @return void 99 | */ 100 | #[\ReturnTypeWillChange] 101 | public function offsetSet($offset, $value) 102 | { 103 | $this->$offset = $value; 104 | } 105 | 106 | /** 107 | * Unset the offset. 108 | * 109 | * @param mixed $offset 110 | * @return void 111 | */ 112 | #[\ReturnTypeWillChange] 113 | public function offsetUnset($offset) 114 | { 115 | // 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /src/Laracasts/Flash/OverlayMessage.php: -------------------------------------------------------------------------------- 1 | message($message, $level); 18 | } 19 | 20 | return $notifier; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/views/message.blade.php: -------------------------------------------------------------------------------- 1 | @foreach (session('flash_notification', collect())->toArray() as $message) 2 | @if ($message['overlay']) 3 | @include('flash::modal', [ 4 | 'modalClass' => 'flash-modal', 5 | 'title' => $message['title'], 6 | 'body' => $message['message'] 7 | ]) 8 | @else 9 | 24 | @endif 25 | @endforeach 26 | 27 | {{ session()->forget('flash_notification') }} 28 | -------------------------------------------------------------------------------- /src/views/modal.blade.php: -------------------------------------------------------------------------------- 1 | 20 | --------------------------------------------------------------------------------