Flash Message
Objective
In the previous section, we have an understanding of the Laravel front-end assets, Laravel's front-end management tool: Elixir + Gulp. This section goes back to PHP and explains the use of Flash message.
Description
Development environment: Windows 7
Laravel version: 5+
Ide:phpstorm
What is flash message, flash, flashing, fleeting, message, messages. Approximate literal translation is "flash", which was originally from rails, used to display some information on the page. Introduce this concept in Laravel and make a hint about the information you need on the page.
After the user has done something on your website, you need to respond to the behavior. For example, if you register, you have to prompt for "registered success", not the effect of JS alert (), but the intention of alert ().
It may be used in our blog system. Party after we create a new article, we will immediately jump to the article List page, more abrupt. A hint how good, prompt me to create complete--although I know the creation is complete.
Add Flash Message
First, open the articlescontroller.php and find the Store () method, where we implement the method of storing the written article. The code is as follows:
Public function Store (articlerequest $request) { $article = new article ($request->all ()); Auth::user ()->articles ()->save ($article); Return redirect (' articles ');
When the article is created successfully, the page will be redirect to/articles, at which point we can create a flash message object in the session, wrapped 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 can not be shown, just add a key value pair in the session, want to show that you have to extract v according to K. Here the session has a flash () method, because the Laravel framework is written, you can also write a session::p UT, the difference between this and Flash is that Flash is "affair", a request for a flash MESSAG E, when you reload the page, the flash message disappears, but the put is different.
Show Flash Message
We come to our master.blade.php, which defines 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 logical statement to the previous content to determine if there is a key named ' Flash_message ' in the Session object, and some words are extracted and displayed on the content.
At this point, our flash message J can be displayed. Start the server, Access Localhost:8888/articles/create after landing, post the article, and then you will see the green tips on top. When you reload the page, the hint disappears. Flash ~
Fade Flash Message
Yes, every time reload page, what is this pit-daddy approach? What to do? Then add a fork to it, or disappear at a timed time.
Fork out Flash Message
First, look at the fork.
Add such a sentence in the blade just now:
@if (Session::has (' flash_message '))
x {{session::get (' Flash_message ')}} @endif @yield ( ' content ') @yield (' footer ')
Do two actions, first, the following introduction of two JS files, the second in the above to add a button control code, meaning is also very clear.
Then go to the browser to access Localhost:8888/articles/create, after creation, you can see there is a small fork in the tip of the upper right corner, and after the click disappears, eliminating the reload page.
Auto Fade Flash Message
The following logic, the hint is divided into two, one is important, one is not important, it is not important to let its own time to disappear, it is important to let users go to Point fork.
OK, here's a look at how it's implemented:
@if (Session::has (' flash_message ')) @if (Session::has (' flash_message_important '))
x @endif {{session::get (' Flash_message ')}} @endif @yield (' content ') @yield (' footer ')
First, in
Here to the key in the session to make a judgment, if there is a key named Flash Messageimportant, then to the Div continue to add a Alert-important class property, otherwise not added.
@if (Session::has (' flash_message_important '))
x @endif
The meaning of the sentence is that if there is an important flash_message, then a fork is displayed.
Finally added a section of jquery js, used to automatically disappear Div. If the logic is not important, the 3s disappears automatically after the display.
You can look at the effect after you've created it in Localhost:8888/articles/create.
If you want to look at the effect of the fork, go back to articlescontroller.php, add a sentence below the \seesion
\session::flash (' Flash messageimportant ', true); Save and create an article try ~
Another way to add flash message
There is one more way to 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 OK, too.
Summarize
Flash mesage as a hint in the web design to play a human side, to the user more choice, more intimate, better interaction.