【问题标题】:Retrieve last item in Session::检索会话中的最后一项::
【发布时间】:2014-12-09 04:55:48
【问题描述】:

我想知道如何检索名为 smartBacklinks 的 Session 中的最后一个实例。

这里是代码

if(Session::has('smartBacklinks'))
{
    // if(Request::header('referer') === LAST ITEM IN SESSION[smartBacklinks] ARRAY)
    Session::push('smartBacklinks', Request::header('referer'));
}
else
{
    Session::put('smartBacklinks', [Request::header('referer')]);
}

另外我如何从刀片模板中检索它?

【问题讨论】:

    标签: php session laravel laravel-4 blade


    【解决方案1】:

    我对我的代码进行了很多编辑,现在它可以运行了。我仍然需要添加一些调整以使其 100% 符合我的需要

    代码现在看起来像这样:

    if(Session::has('smartBacklinks')){
        // Get the last item in Session array 
        $slice = array_slice(Session::get('smartBacklinks'), -1, 1);
    
        // Check if Request::header('referer') is equal to the $slide[0]
        if(Request::header('referer') != $slice[0]){
            // Check if Request::header('referer') is empty
            if(Request::header('referer') != '') Session::push('smartBacklinks', Request::header('referer'));
        }
        // If session[smartBacklinks] is not set. - Set
        }else {
            Session::put('smartBacklinks', [Request::header('referer')]);
    
            $slice = array_slice(Session::get('smartBacklinks'), -1, 1);
        }
    
    Session::save();
    

    那么会话数组的最后一个实例当然是

    $slice[0]
    

    我需要补充的最后一件事是:

    • 单击“后退按钮”时,应删除会话数组的最后一个实例,并且不应将 URL 推送到会话中
    • 我需要确保会话已正确加载,因此我不必刷新网页即可获得正确的“返回 URL”

    感谢您的回答!

    【讨论】:

      【解决方案2】:

      您可以根据密钥从会话中检索“smartBacklinks”,如下所示:

      $value = Session::get('smartBacklinks');
      

      另外,您可能需要注意,您将使用 Session::push() 推送到数组会话值中,并使用 Session::put() 将项目简单地存储在会话中。

      从刀片中检索值:

      我猜你可以像这样将刚刚从控制器检索到的变量传递给视图:

      return View::make('foo.bar', array('smartBacklinks' => $value));
      

      然后像这样在刀片中使用它:

      <a href="{{ URL::to($smartBacklinks) }}" class="btn btn-mini btn-primary">Go back</a>
      

      希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 2011-01-15
        • 2017-04-10
        • 1970-01-01
        • 1970-01-01
        • 2013-01-05
        • 1970-01-01
        • 1970-01-01
        • 2023-03-07
        • 1970-01-01
        相关资源
        最近更新 更多