[英]Laravel 5.2.32: Session flash doesn't work with redirect
我有一個具有重定向功能的控制器:
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忘記了該會話數據。 我之前已經做過很多次,而且從未見過在一次重定向后是否忘記了會話閃存數據。 這里發生了什么? 我嘗試添加和刪除“ Web”中間件,但沒有任何效果。 如果有人知道為什么會這樣,請告訴我。
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);
}
嘗試這樣。 使用會話並在會話中設置數據,然后從所需位置獲取數據。
在myControllerMethod
您將data
obj / var作為請求傳遞。
在otherControllerMethod
您正在請求未設置的Session數據。
為了將數據放入會話中,您應該執行以下操作:
Session::put('data','value')
然后將可用於:
Session::get('data');
我以前有過同樣的問題。 基本上,在使用Redirect
外觀進行Redirect
時,我需要調用send
函數。 因此,您需要將myControllerMethod
更改為:
public function myControllerMethod()
{
$data = $this->blabla();
return Redirect::to('previousroute')->with('data', $data)->send();
}
由於Symfony\\Component\\HttpFoundation\\Response
類的send()
函數調用了sendContent()
函數,該函數在重定向時發送數據。
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.