PHP Laravel:如何设置或获取会话数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37292167/
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
PHP Laravel: How to set or get Session Data?
提问by User57
I want to store some data in session for some testing purpose. I have written the session code also in controller. But in console.log ->resources -> session I couldn't find any value which I stored. If anyone help me to find the mistake which I have done in my controller please.
我想在会话中存储一些数据用于某些测试目的。我也在控制器中编写了会话代码。但是在 console.log ->resources -> session 我找不到我存储的任何值。如果有人帮我找到我在控制器中犯的错误,请。
Here is my controller:
这是我的控制器:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Department;
use App\Http\Requests;
use Cookie;
use Tracker;
use Session;
public function postdepartmentSave(Request $request)
{
$this->validate($request,[
'code' => 'required|min:2|max:7|unique:departments',
'name' => 'required|unique:departments',
]);
$department = new Department();
$department->code = $request->Input(['code']);
$department->name = $request->Input(['name']);
$name= $department->name;
Session::put('name', $name);
dd($name);
$department->save();
return redirect('departmentSavePage');
}
回答by Tommy Wu
Storing data
存储数据
To store data, you can use:
要存储数据,您可以使用:
Session::put('variableName', $value);
There is also another way, through the global helper:
还有另一种方式,通过全局助手:
session(['variableName' => $value]);
Getting data
获取数据
To get the variable, you'd use:
要获取变量,您可以使用:
Session::get('variableName');
回答by patricus
You haven't made a coding mistake. The issue is that you're getting a little confused between server side sessions and HTML5's sessionStorage.
你没有犯编码错误。问题是您在服务器端会话和 HTML5 的 sessionStorage 之间有点混淆。
The Resources->Session Storage
view on the Chrome Developer Tools is only showing you the information stored in the HTML5 sessionStorage object. This is client side session information that is accessed via client side javascript, and is completely separate from any type of server side session information.
在Resources->Session Storage
Chrome开发者工具视图只显示你存储在HTML5的sessionStorage对象的信息。这是通过客户端javascript访问的客户端会话信息,与任何类型的服务器端会话信息完全分开。
For example, run sessionStorage.setItem('key', 'value');
in your javascript console, and you will see that data show up in the Resources->Session Storage
view.
例如,sessionStorage.setItem('key', 'value');
在您的 javascript 控制台中运行,您将看到数据显示在Resources->Session Storage
视图中。
Your PHP/Laravel code is dealing with server side sessions. This information is not going to show up in any type of Developer Tools view because it is not meant to be accessed by client side tools. By default, Laravel uses file based session storage, which means your session information is stored in files on your server (in storage/framework/sessions
). Even if you change the session storage to use cookies, the session information will be encrypted so that the client side cannot read the data. This is for security purposes.
您的 PHP/Laravel 代码正在处理服务器端会话。此信息不会显示在任何类型的开发人员工具视图中,因为客户端工具不打算访问它。默认情况下,Laravel 使用基于文件的会话存储,这意味着您的会话信息存储在服务器上的文件中(在storage/framework/sessions
. 即使您将会话存储更改为使用 cookie,会话信息也会被加密,客户端无法读取数据。这是出于安全目的。
You can read more about Laravel sessions here.
You can read more about HTML5 sessionStorage here.
您可以在此处阅读有关Laravel 会话的更多信息。
您可以在此处阅读有关HTML5 sessionStorage 的更多信息。