Laravel's session brief API
Session API is relatively simple, we look at the Chinese document also probably know how to mean. But there are a few that are not very well understood.
- Permanent Save of Session (within the non-expiring range)
- Session::p ut (' key ', ' value ');
- Native session equivalent to PHP
- $_session[' key ' = ' value ';
- Get operation
- $value = Session::get (' key ', ' default ');
- Remove operation and Delete, similar to pop concept
- $value = Session::p ull (' key ', ' default ');
- Detect if a key exists
- Session::has (' users ');
- Delete key
- Session::forget (' key ');
Set the value in session
Public Function Session1 (Request $request) {
1.HTTP request session ();
/* $request->session ()->put (' Key1 ', ' value1 ');
echo $request->session ()->get (' Key1 '); */
2.session ()
/*session ()->put (' Key2 ', ' value2 ');
echo session ()->get (' Key2 '); */
3.session
storing data to session
Session::p ut (' Key3 ', ' value3 ');
Get the data in session
echo session::get (' Key3 ');
Does not exist then gets the default value
echo session::get (' Key4 ', ' Default ');
Storing data as an array
Session::p ut ([' key4 ' = ' value4 ']);
Put the data in the array of the session
/*session::p ush (' user ', ' yxh ');
Session::p ush (' user ', ' IMOOC '); */
Extracting data from an array
/* $res = session::get (' user ', ' default ');
Var_dump ($res); * *
Remove the data and delete it
/* $res = Session::p ull (' user ', ' default ');
Var_dump ($res); * *
Take out the ownership in the session
/* $res = Session::all ();
Var_dump ($res); * *
Determine if a value exists in the session
/*if (Session::has (' Key1 ')) {
$res = Session::all ();
DD ($res);
}else{
Echo ' does not exist ';
}*/
Delete the value specified in the session
Session::forget (' Key1 ');
Clear all session
Session::flush ();
Set temporary data, access only once, second access deletes
Session::flash (' Key-flash ', ' Flash ');
}
Laravel 4.2 Session