【问题标题】:Laravel 5.2.32: Session flash doesn't work with redirectLaravel 5.2.32:会话闪存不适用于重定向
【发布时间】:2016-06-23 20:51:41
【问题描述】:

我有一个具有重定向功能的控制器:

public function myControllerMethod() 
{
    $data = $this->blabla();

    return Redirect::to('previousroute')->with('data', $data);
}

这个先前的路由由 otherControllerMethod() 处理,如下所示:

public function otherControllerMethod()
{
    $data = Session::get('data');

    return $this->makeView($data);
}

不幸的是,Laravel 忘记了这个会话数据。我以前做过很多次,我从未见过在一次重定向后是否忘记了会话闪存数据。这里发生了什么?我已经尝试添加和删除“网络”中间件,但没有任何效果。如果有人知道为什么会这样,请告诉我。

【问题讨论】:

  • 我遇到过几次类似的问题。您能否检查问题是否在于您每次请求都会获得一个新会话?就我而言,问题与清漆有关。 Varnish 没有绕过 cookie 到 ngnix,每次都会变成一个新会话。在这种情况下有几个解决方法:调整你的清漆设置或添加一个特殊的标题来强制清漆绕过 cookie。希望它带来一些光明。
  • 您显示的代码均未设置任何会话或闪存数据。
  • 帕特里克斯 - 这实际上不是真的。使用 with() 方法的 redirect() 会刷新数据。见:laravel.com/docs/5.2/…

标签: php laravel session redirect middleware


【解决方案1】:
use Session;

public function myControllerMethod() 
{
    $data = $this->blabla();
    Session::set('data', $data);
    return Redirect::to('previousroute')->with('data', $data);
}

public function otherControllerMethod()
{
    $data = Session::get('data');

    return $this->makeView($data);
}

试试这样。使用会话并在会话中设置数据并从您想要的位置获取。

【讨论】:

    【解决方案2】:

    我之前也遇到过同样的问题。基本上我需要在使用Redirect 外观重定向时调用send 函数。因此,您需要将您的myControllerMethod 更改为:

    public function myControllerMethod() 
    {
        $data = $this->blabla();
        return Redirect::to('previousroute')->with('data', $data)->send();
    }
    

    由于Symfony\Component\HttpFoundation\Response类的send()函数调用了sendContent()函数,该函数在重定向时发送数据。

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      myControllerMethod 中,您将data obj/var 作为请求传递。

      otherControllerMethod 中,您正在请求未设置的会话数据。

      为了将数据放入会话中,您应该这样做:

      Session::put('data','value')
      

      然后它将可用:

      Session::get('data');
      

      【讨论】:

        猜你喜欢
        • 2019-05-19
        • 2021-11-29
        • 1970-01-01
        • 2018-05-02
        • 1970-01-01
        • 1970-01-01
        • 2018-02-28
        • 2019-07-01
        • 2018-08-03
        相关资源
        最近更新 更多