2016-04-29 51 views
0

很基本的事情正在進行。使用Session Flash消息顯示任何成功/錯誤消息。會話flash不能正常工作,放入[L5.2]

所有路線都在我的'middleware' => 'web'組中設置。

現在我的問題。 ->flash()不起作用,而->put()

控制器:

$request->session()->flash('alert-success', 'My flash message'); 

刀片:

@foreach (['danger', 'warning', 'success', 'info'] as $msg) 
    @if(Session::has('alert-' . $msg)) 
     <p class="alert alert-{{ $msg }}">{{ Session::get('alert-' . $msg) }} <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a> 
     </p> 
    @endif 
@endforeach 

Laravel 5 docs陳述的return redirect('routeHere')->with('alert-success', 'My flash message'); 也不起作用。

編輯

Laravel 5.2 Session flash not working even with web middleware重複。使用['middlewareGroups' => 'web']而不是['middleware' => 'web']確實解決了這個問題,但假設這不是要走的路。

+1

您確定要在最近的下一個請求中使用它。因爲'flash'只適用於下一個請求......並且如果你想在稍後的任何請求中獲得這個消息,你應該使用'put'。 –

+0

是的,我想要那個。同樣的問題在這裏:http://stackoverflow.com/questions/36279871/laravel-5-2-session-flash-not-working-even-with-web-middleware?rq=1(使用'middlewareGroups'而不是'中間件'可以工作,但不是我想的方式。 – Liam

+0

你可以在視圖中轉儲:'{{dd($ request-> session() - > all())}}' –

回答

0

經過對這個問題的好奇搜索後,我注意到laravel version >= 5.2.27有趣的更新,默認情況下,網絡中間件組適用於routes.php中定義的所有路由。

現在,這讓我認爲通過聲明中間件['middleware' => 'web']將導致代碼被調用兩次,從而清除非持久性Flash消息。

基於我的假設一個解決方案是根本不使用:

Route::group(['middleware' => 'web'], function() { 
}); 

這是我發現的更新信息:) Laravel 5.2 Auth not Working

0

嘗試在laravel刪除「網絡」中間件5.2來自你的routes.php文件。這種方式可以幫助我