[Laravel5Fundamentals] 20-FlashMessage Flash Message
Preface
In the previous section, we learned about Laravel's front-end assets. Laravel's front-end management tool is elixir + gulp. This section introduces the use of flash message in php.
Description
Development Environment: Windows 7
Laravel version: 5 +
IDE: Phpstorm
What is flash message, flash, flashing, transient, message, and message. Probably, the literal translation is "flash", which was originally from rails and used to display some prompt information on the page. Introduce this concept in Laravel to prompt the information required on the page.
After a user performs a behavior on your website, you need to respond to the behavior. For example, if you have registered, you have to prompt "successful registration", which is not the effect of js alert (), but the intention of alert.
It may be used in our blog system. After we create an article, we will jump to the article list page immediately, which is abrupt. A prompt is displayed, indicating that the creation is complete-although I know that the creation is complete.
Add flash message
First, open ArticlesController. php and find the store () method. the method for storing written articles is here. The code is as follows:
public function store(ArticleRequest $request) { $article = new Article($request->all()); Auth::user()->articles()->save($article); return redirect('articles'); }
After the article is created successfully, the page will be redirect to/articles. at this time, we can create a flash message object in the session and wrap it in a k-v:
public function store(ArticleRequest $request) { $article = new Article($request->all()); Auth::user()->articles()->save($article); \Session::flash('flash_message','Your article has been created!'); return redirect('articles'); }
At this point, it cannot be displayed, but a key-value pair is added to the session. to display the key-value pair, extract v based on k. Here, the session has a flash () method because it has been written in the Laravel framework. you can also write a Session: put. The difference between this and flash is that, flash is a "one-click transaction". one request is a flash message. when you reload the page, the flash message disappears, but put is different.
Display flash message
We came to our master. blade. php and defined the format of all our layout files. Here we add the following code:
@if (Session::has('flash_message'))
{{Session::get('flash_message')}}
@endif @yield('content')
@yield('footer')
Add a logic statement to the original content to determine whether the session object contains a key named 'flash _ message'. If yes, the key is extracted and displayed on the content.
So far, our flash message j can be displayed. Start the server, access localhost: 8888/articles/create, post an article, and then you will see the green prompt on the top. When you reload the page, the prompt disappears. Flash ~
Flash message disappears
That's right. what is the best practice for every reload page? What should I do? Add a cross sign to it, or regularly disappear.
Flash message removal
First look at the cross.
Add the following sentence to the blade:
@if (Session::has('flash_message'))
× {{Session::get('flash_message')}}
@endif @yield('content')
《script》
《script》
@yield('footer')
I did two operations: first, introduce two js files below, and then add a piece of code for the button control above, which is quite clear.
Visit localhost: 8888/articles/create in the browser. after the creation is complete, you can see that there is a cross in the upper right corner of the prompt, and the click disappears, saving the reload page.
Flash message automatically disappears
In the following logic, there are two types of prompts: one is important, the other is unimportant, and the other is unimportant, so that it disappears by time, it is important for users to calculate the cross.
Okay. let's see how it works:
@if (Session::has('flash_message'))
@if (Session::has('flash_message_important'))
× @endif {{Session::get('flash_message')}}
@endif @yield('content')
《script》
《script》《script》 $('p.alert').not('.alert-important').delay(3000).slideUp(300);《script》
@yield('footer')
First
Here, we will make a judgment on the key in the session. if there is a key named flash messageimportant, we will continue to add an alert-important class attribute to p. Otherwise, we will not add it.
@if (Session::has('flash_message_important'))
×@endif
In other words, if there is an important flash_message, a cross sign is displayed.
Finally, a piece of jquery js is added to automatically disappear p. If the logic is not an important message, it will automatically disappear after three seconds.
You can check the effect after creating localhost: 8888/articles/create.
If you want to see the cross effect, return to ArticlesController. php and add a sentence under \ Seesion.
\ Session: flash ('flash messageimpant ant', true); save and create an article ~
Another method of adding flash message
You can also add a session. Open ArticlesControler. php and add the following code:
public function store(ArticleRequest $request) { $article = new Article($request->all()); Auth::user()->articles()->save($article); //\Session::flash('flash_message','Your article has been created!'); //\Session::flash('flash_message_important',true); return redirect('articles')->with([ 'flash_message'=>'Your article has been created!', 'flash_message_important'=>true]); }
This is also OK.
Summary
As a reminder, flash mesage plays a user-friendly aspect in web design, giving users more choices, more caring, and better interaction.