【问题标题】:Session issue with Laravel 4Laravel 4 的会话问题
【发布时间】: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


【解决方案1】:

如果人们花时间在这个问题上,我很抱歉,尤其是亚当,但我发现了我的错误。这是布局中的错字,我有这个{ URL::to('/') }} 注意缺少的第一个花括号。这仅在 IE 中中断会话。 谢谢。

【讨论】:

    猜你喜欢
    • 2014-03-31
    • 1970-01-01
    • 2015-04-26
    • 2015-10-08
    • 2014-12-08
    • 2016-01-16
    • 2015-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多