访问控制器中的会话数据(Laravel) [英] Accessing session data in controller(Laravel)
问题描述
我有一个页面"A".我在此页面上创建一个会话变量.
I have a page 'A'. I create a session variable on this page.
然后使用window.location
将该页面重定向到控制器'A'中的功能'A'.
This page is then redirected to a function 'A' in a controller 'A' using window.location
.
我尝试使用以下行访问函数'A'中的会话变量
I try to access the session variable in the function 'A' using the following line
var_dump($request->session->get('variableSetOnPageA'));
这将返回NULL
.
为什么?我需要'variableSetOnPageA'.
Why? I need the 'variableSetOnPageA'.
推荐答案
您还可以在Laravel中获取Session
变量,如下所示,在Controller文件中的任何函数中
You can also get Session
variable in Laravel like below in any of your function in Controller file:
$value = Session::get('variableSetOnPageA');
您可以在任何函数中像下面一样设置Session
变量:
And you can set your Session
variable like below in any of your function:
$variableSetOnPageA = "Can be anything";
Session::put('variableSetOnPageA',$variableSetOnPageA);
确保在Controller文件中,在顶部添加以下代码:
In your Controller file, make sure you add below code at top:
use Session;
这篇关于访问控制器中的会话数据(Laravel)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!