将值附加到现有会话Laravel 5 [英] Append value to existing session Laravel 5
问题描述
一旦登录,我就将用户值设置为会话.
Once in my login I set values of user to session like this..
session()->set("user", $user);
为了从我的应用程序中的任何位置获取会话的值,请将其附加到$request
$request->_user = $user;
,如下所示.所以我可以从任何地方得到这样的东西
And for get the values of the session from anywhere in my application attach with it to $request
$request->_user = $user;
like this. So I can get it like this from anywhere
$user = $request->_user;
我要在应用程序的中部传递一些采购订单ID.因此,我可以将其附加到现有会话或如何创建新会话并附加ID.使用该ID,页面重定向到Paypal,如果用户取消了付款,我可以使用该ID来捕获他取消的付款.这就是为什么我认为会议对我有帮助.成功付款后,可以销毁该会话.救救我!
Thing is I want pass some ID of purchase order at the midle of the application. So can I attach it to existing session or how to create new session and append the ID. With that ID, page redirect to paypal and If user cancel the payment I can catch his canceled payment with that ID. That is why I thought session would help me. After a successful payment it is OK to destroy that session. Help me !!!
推荐答案
您可以在用户使用应用程序时继续将数据追加到会话中.假设您要存储一组购买ID,可以执行以下操作:
You can continue to append data to the session while the user is using your application. Assuming you want to store a array of purchase IDs, you can do:
session()->push('item_ids', 'item1');
session()->push('item_ids', 'item3');
这将创建一个数组,以便您以后可以保存用户尝试购买的所有ID:
This will create an array so you can later save all the IDs that the user attempted to purchase:
$items = session('item_ids');
这将返回一个数组,其中包含您已推送到该数组的所有item_ids
:
This will return an array with all the item_ids
you've pushed to the array:
[
'item1',
'item3'
]
成功购买后或保存已取消的购买详细信息后,只需开始新的会话或删除item_id.
After successful purchase or after you've saved the canceled purchase details, just start a new session or delete the item_ids.
// generate new session
session()->regenerate();
// delete item_ids
session()->forget('item_ids');
这篇关于将值附加到现有会话Laravel 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!