会话在 Laravel 5.3 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41398188/
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
Session not working in laravel 5.3
提问by UMAIR ALI
I am trying to create session like below code but session not working please sussgest me any soluton.
我正在尝试像下面的代码一样创建会话,但会话不起作用,请向我提供任何解决方案。
Save
节省
$data = array(
"id" => $row->id,
"name" => $row->name
);
Session($data);
Fetch
拿来
Session('id');
I also tried web middleware but till the same session not working
我也尝试过网络中间件,但直到同一个会话不起作用
Route::group(['middleware' => ['web']], function ()
{
});
回答by Alexey Mezenin
You should remove web
middleware from routesto fix the problem with sessions.
Also correct syntax for persisting data is:
持久化数据的正确语法是:
session(['key' => $data]);
To get data use:
获取数据使用:
session('key');
回答by sabuz
Session will be working fine if the following step can be followed...
如果可以遵循以下步骤,会话将正常工作......
First Step:
第一步:
Add the following code inside a controller(where Session will be used to save data)
在控制器中添加以下代码(其中 Session 将用于保存数据)
use Session;
Second Step:
第二步:
Inside a method of that controller, Session code like below:
在该控制器的方法中,会话代码如下:
Session::put('name', 'Sabuz');
Session::put('data', $data);
any data can be saved but make sure first parameter of put method is key and second is its value
任何数据都可以保存,但要确保 put 方法的第一个参数是键,第二个是它的值
Third Step:
第三步:
That data can be viewed from anywhere with the below command as long as session caries that data
只要会话包含该数据,就可以使用以下命令从任何地方查看该数据
$name = Session::get('name'); //get method just use the key of a put method
echo $name;
Hopefully, it will be workable.
希望它是可行的。
回答by Mayank Dudakiya
I have recently solved this issue.
我最近解决了这个问题。
If you are trying to save array in session or if you want to push data into the session this try following.
如果您尝试在会话中保存数组,或者您想将数据推送到会话中,请尝试以下操作。
Solution:
解决方案:
First go to Kernel.php into the "App\Http\Kernel.php" and add this line \Illuminate\Session\Middleware\StartSession::class,
into the $middleware array. This will start storing the data.
首先将 Kernel.php 转到“App\Http\Kernel.php”并将此行添加\Illuminate\Session\Middleware\StartSession::class,
到 $middleware 数组中。这将开始存储数据。
For session array
对于会话数组
Session::push('cart', $product);
Session::push('cart', $product);
For Single value
对于单值
Replace session_key with the variable you want.
用你想要的变量替换 session_key。
Session::put('session_key');
Session::put('session_key');
回答by namal
If you want persistence sessions, use session()->save()
or Session::save()
如果您想要持久性会话,请使用session()->save()
或Session::save()
$data = array("id" => $row->id, "name" => $row->name);
session($data);
//or
session()->put('key', 'value');
session()->save();
echo session('id');
Also, the 'storage' directory should have write permission.
此外,“存储”目录应该具有写权限。
chmod -R a+rw storage/
回答by UMAIR ALI
My session is not working because I tried to put and fetch in controller constructor and Laravel 5.3 not supporting directly put and fetch session in a constructor. If you want to put and fetch session in a constructor you need to add below code in a constructor.
我的会话不起作用,因为我试图在控制器构造函数中放置和获取,而 Laravel 5.3 不支持在构造函数中直接放置和获取会话。如果要在构造函数中放置和获取会话,则需要在构造函数中添加以下代码。
function __construct()
{
$this->middleware(function ($request, $next)
{
});
}
回答by MrChux
Save the data
保存数据
session()->put('data' => $data);
Get the data
获取数据
session()->get('data');
回答by Md. Abu Taleb
Use session like
使用会话
use Illuminate\Support\Facades\Session;
Set Session:
设置会话:
Session::flash('key', 'Value');
View File :
查看文件 :
@if(Session::has('key'))
<div class="alert-success">
{{ Session::get('key') }}
</div>
@endif
Reference: https://laravel.com/docs/5.3/facades#facade-class-referencehttps://laravel.com/api/5.3/Illuminate/Support/Facades/Session.html
参考:https: //laravel.com/docs/5.3/facades#facade-class-reference https://laravel.com/api/5.3/Illuminate/Support/Facades/Session.html