在 Laravel 中设置会话变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34488194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set session variables in Laravel
提问by moh_abk
In vanilla PHP, I can set a session variable using the below code;
在普通 PHP 中,我可以使用以下代码设置会话变量;
$_SESSION['blog'] = array(
"email" => $_POST['email'],
"password" => $password,
"firstname" => $row ['firstName'],
"lastname" => $row ['lastName'],
"id" => $row ['id']
);
How can I do the same in laravel
.
FYI I'm not talking about the default session name eg PHPSESSID
or laravel_session
. I have multiple session variables for parts of my site such as web
, blog
, 'careers' and I name them just like the code above and able to check and access them individually. So one user can have all three sessions variables at the same time but for different sections of the site.
我怎样才能在laravel
. 仅供参考,我不是在谈论默认会话名称,例如PHPSESSID
或laravel_session
. 我的网站的某些部分有多个会话变量,例如web
, blog
, 'careers' 并且我像上面的代码一样命名它们,并且能够单独检查和访问它们。因此,一个用户可以同时拥有所有三个会话变量,但适用于站点的不同部分。
回答by lagbox
One way you could do it:
你可以这样做的一种方法:
$vars = [
"email" => $email,
"password" => $password,
"firstname" => $row['firstName'],
"lastname" => $row['lastName'],
"id" => $row['id']
];
session()->put('blog', $vars);
Update:To check existence of a session variable:
更新:检查会话变量的存在:
session()->has('blog'); // bool
Update:To get an element contained in the session variable 'blog'
更新:获取包含在会话变量“博客”中的元素
session('blog.id'); // using the dot notation
回答by Haseena P A
//Place the array in a variable
$session_array = array(
"email" => $_POST['email'],
"password" => $password,
"firstname" => $row ['firstName'],
"lastname" => $row ['lastName'],
"id" => $row ['id']
);
//Place that $session_array inside the session using \Session::put('key','value')
\Session::put('blog',$session_array);
// To access the session value
print_r(\Session::get('blog'));