1- # Easy Flash Messages
1+ # Easy Flash Messages for Your Laravel App
22
33## Installation
44
@@ -14,68 +14,41 @@ And then, if using Laravel 5, include the service provider within `config/app.ph
1414];
1515```
1616
17- And, for convenience, add a facade alias to this same file at the bottom:
18-
19- ``` php
20- 'aliases' => [
21- 'Flash' => Laracasts\Flash\Flash::class,
22- ];
23- ```
24-
2517## Usage
2618
2719Within your controllers, before you perform a redirect...
2820
2921``` php
3022public function store()
3123{
32- Flash::message ('Welcome Aboard!');
24+ flash ('Welcome Aboard!');
3325
34- return Redirect:: home();
26+ return home();
3527}
3628```
3729
3830You may also do:
3931
40- - ` Flash::info('Message') `
41- - ` Flash::success('Message') `
42- - ` Flash::error('Message') `
43- - ` Flash::warning('Message') `
44- - ` Flash::overlay('Modal Message', 'Modal Title') `
32+ - ` flash('Message', 'info') `
33+ - ` flash('Message', 'success') `
34+ - ` flash('Message', 'danger') `
35+ - ` flash('Message', 'warning') `
36+ - ` flash()->overlay('Modal Message', 'Modal Title') `
37+ - ` flash('Message')->important() `
4538
46- Again, if using Laravel , this will set a few keys in the session:
39+ Behind the scenes , this will set a few keys in the session:
4740
4841- 'flash_notification.message' - The message you're flashing
4942- 'flash_notification.level' - A string that represents the type of notification (good for applying HTML class names)
5043
51- Alternatively, again, if you're using Laravel, you may reference the ` flash() ` helper function, instead of the facade. Here's an example:
52-
53- ``` php
54- /**
55- * Destroy the user's session (logout).
56- *
57- * @return Response
58- */
59- public function destroy()
60- {
61- Auth::logout();
62-
63- flash()->success('You have been logged out.');
64-
65- return home();
66- }
67- ```
68-
69- Or, for a general information flash, just do: ` flash('Some message'); ` .
70-
7144With this message flashed to the session, you may now display it in your view(s). Maybe something like:
7245
7346``` html
74- @if (Session:: has('flash_notification.message'))
75- <div class =" alert alert-{{ Session::get ('flash_notification.level') }}" >
47+ @if (session()-> has('flash_notification.message'))
48+ <div class =" alert alert-{{ session ('flash_notification.level') }}" >
7649 <button type =" button" class =" close" data-dismiss =" alert" aria-hidden =" true" >× ; </button >
7750
78- {{ Session::get ('flash_notification.message') }}
51+ {{ session ('flash_notification.message') }}
7952 </div >
8053@endif
8154```
@@ -106,10 +79,10 @@ Because flash messages and overlays are so common, if you want, you may use (or
10679 <p >Welcome to my website...</p >
10780</div >
10881
82+ <!-- This is only necessary if you do Flash::overlay('...') -->
10983<script src =" //code.jquery.com/jquery.js" ></script >
11084<script src =" //maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js" ></script >
11185
112- <!-- This is only necessary if you do Flash::overlay('...') -->
11386<script >
11487 $ (' #flash-overlay-modal' ).modal ();
11588 </script >
@@ -127,27 +100,39 @@ php artisan vendor:publish
127100The two package views will now be located in the ` app/views/packages/laracasts/flash/ ` directory.
128101
129102``` php
130- Flash::message ('Welcome aboard !');
103+ flash ('Welcome Aboard !');
131104
132- return Redirect:: home();
105+ return home();
133106```
134107
135108![ https://dl.dropboxusercontent.com/u/774859/GitHub-Repos/flash/message.png ] ( https://dl.dropboxusercontent.com/u/774859/GitHub-Repos/flash/message.png )
136109
137110``` php
138- Flash::error ('Sorry! Please try again.');
111+ flash ('Sorry! Please try again.', 'danger ');
139112
140- return Redirect:: home();
113+ return home();
141114```
142115
143116![ https://dl.dropboxusercontent.com/u/774859/GitHub-Repos/flash/error.png ] ( https://dl.dropboxusercontent.com/u/774859/GitHub-Repos/flash/error.png )
144117
145118``` php
146- Flash:: overlay('You are now a Laracasts member!');
119+ flash()-> overlay('Notice', 'You are now a Laracasts member!');
147120
148- return Redirect:: home();
121+ return home();
149122```
150123
151124![ https://dl.dropboxusercontent.com/u/774859/GitHub-Repos/flash/overlay.png ] ( https://dl.dropboxusercontent.com/u/774859/GitHub-Repos/flash/overlay.png )
152125
153126> [ Learn exactly how to build this very package on Laracasts!] ( https://laracasts.com/lessons/flexible-flash-messages )
127+
128+ ## Hiding Flash Messages
129+
130+ 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.
131+
132+ ```
133+ <script>
134+ $('div.alert').not('.alert-important').delay(3000).fadeOut(350);
135+ </script>
136+ ```
137+
138+ 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