【发布时间】:2014-02-19 19:17:49
【问题描述】:
从昨天开始,我一直被一个与 Laravel 4 中的会话相关的问题所困扰。我有一个非常简单的应用程序,它是一个车辆制造商,你从一个底座/底盘开始,你可以向它添加选项来构建你的车辆。我有一个(安静的) VehicleController 控制器,其动作如下所示:
public function getShow($id)
{
$excluded_parts = array();
$the_vehicle = Session::get('the_vehicle');
$the_vehicle_built_image = Session::get('the_vehicle_built_image');
// Check if the selected vehicle changed
if ($the_vehicle != $id) {
// Forget the previous sessions
Session::put('the_vehicle', '');
Session::put('the_vehicle_built_image', '');
Session::put('the_options', array());
.......
另一种方法是添加选项:
public function getAddopt($id)
{
// Get the current selected vehicle
$the_vehicle = Session::get('the_vehicle');
// Get the current selected vehicle's options
$the_options = Session::get('the_options', array());
........
奇怪的行为如下,当我第一次加载vehicles/show/1页面时,似乎没有设置会话,当我尝试添加一个选项vehicles/addopt/34时,我立即得到一个错误Session::get('the_vehicle')应该在上一个动作中设置为空。
此问题发生在第一次加载新会话时(我的意思是我需要关闭/重新打开浏览器才能重现)。
我错过了在 Laravel 4 中使用会话的东西吗?
谢谢
【问题讨论】:
-
我不确定我是否理解这个问题。您在与
Session::put的会话中将值存储在哪里?我只看到一个你试图忘记上一个会话的实例,在这种情况下,你应该对所有项目使用Session::forget('the_vehicle');或Session::flush();。 -
值被存储了,只是在
getShow动作后面一点,这个方法很长,我没有粘贴整个东西。Session::forget是正确的,这就是我以前使用的,但尝试调试认为它们可能是它的问题,我尝试使用Session::put使用空字符串进行更新 -
啊,我明白了 - 只是为了确认一下,你是在
Session::put之后打电话给Session::save()吗? -
感谢您的帮助亚当。
Session::save()?不,我不使用它,也没有在文档中看到它laravel.com/docs/session -
你能告诉我们错误吗?
标签: php session laravel session-variables