【问题标题】:Laravel 5 custom helper could not set sessionLaravel 5 自定义助手无法设置会话
【发布时间】:2017-11-06 20:21:35
【问题描述】:

我的 Laravel 项目中有自定义助手类:

<?php

namespace App\Http\Helpers;

class FoxUtils {

  public static function isAuthTo($name)
  {
     if (self::test($name)){
       \Session::push('AuthList',[$name => true]);
       return true;
     }
     else{
      \Session::push('AuthList',[$name => false]);
       return false;
     }
  }
}

有两个注意事项:

会话变量AuthList 是数字索引数组,其值如下所示:

array:2 [▼
  0 => array:1 [▼
    "name1" => true
  ]
  1 => array:1 [▼
    "newName" => true
  ]
]

"name1" =&gt; true 是从我的助手以外的其他地方定义的。当我尝试使用助手的方法时,我尊重应该将新键添加到数组中:

\FoxUtils::isAuthTo('AnotherName');
dd(session('AuthList'))

上面的代码打印的数组只有两个键,而我期望三个键:

array:2 [▼
      0 => array:1 [▼
        "name1" => true
      ]
      1 => array:1 [▼
        "AnotherName" => true
      ]
    ]

换句话说,AuthList 的最后一个值总是被新值替换!这里有什么问题?

【问题讨论】:

    标签: arrays session laravel-5 laravel-5.4


    【解决方案1】:

    来自this,我尝试使用Session::save(),效果很好:

    ....
    if (self::test($name)){
           \Session::push('AuthList',[$name => true]);
           \Session::save();
           return true;
         }
    ....
    

    【讨论】:

      猜你喜欢
      • 2018-11-21
      • 2017-08-28
      • 1970-01-01
      • 2021-04-09
      • 1970-01-01
      • 2019-02-02
      • 1970-01-01
      • 2018-02-11
      • 2017-11-09
      相关资源
      最近更新 更多