2016-04-01 24 views
1

我試圖在Laravel提交表單時顯示成功消息。Session :: flash在Laravel 5.2.27版本中不起作用

但是,它不起作用。

我在控制器文件的頂部添加了use Session;,路由在中間件中,config/session.php中的配置是默認配置。

我的控制器功能能夠在數據庫中保存沒有任何問題:

public function store(Request $request) 
    { 

     $post = new Post; 
     $post->title = $request->title; 
     $post->description = $request->description; 
     $post->slug = $request->slug; 
     $post->body = $request->body; 

     $post->save(); 

     Session::flash('success', 'SUCCESS MESSAGE GOES HERE'); 

     return redirect()->route('posts.show', $post->id); 

    } 

這裏是我的模板文件:

@if(Session::has('success')) 
<div class="alert-box success"> 
    <h2>{{ Session::get('success') }}</h2> 
</div> 
@endif 

我的路線:

Route::group(['middleware' => ['web']], function() { 
Route::get('/', '[email protected]'); 
Route::resource('posts','PostController'); 
}); 

是看會話文件沒有成功。我不明白爲什麼:

a:4:{s:6:"_token";s:40:"EntXIr9tkqAcKarDZhaNxKb6RfcFdFV9ZtF6W7kU";s:9:"_previous";a:1:{s:3:"url";s:30:"http://localhost:8000/posts/35";}s:5:"flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}s:9:"_sf2_meta";a:3:{s:1:"u";i:1459467699;s:1:"c";i:1459467699;s:1:"l";s:1:"0";}} 

有人可以幫助我找出問題是什麼?

+0

Laravel 5.1或5.2? –

+0

Laravel版本5.2.27 –

+0

是否爲全新安裝?你的routes.php文件是什麼? – scrubmx

回答

0

確保路由使用「web」中間件訪問會話數據。

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

    Route::get('foo', '[email protected]'); // session data should be available here 
}); 
+0

是的,我認爲這是問題,有[最近的變化](https://github.com/laravel/laravel/commit/5c30c98db96459b4cc878d085490e4677b0b67ed)他們提取中間件到[RouteServiceProvider] (https://github.com/laravel/laravel/blob/5c30c98db96459b4cc878d085490e4677b0b67ed/app/Providers/RouteServiceProvider.php#L54)。 – scrubmx

+0

@scrubmx感謝您的更新。 –

+0

嗨,是的,我的路線是在組中間件。幾分鐘前,我已經用這個信息更新了這個問題。 –

1

web中間件不再需要明確地應用到路線在routes.php文件的文件。它現在默默Laravel應用到路線內app/Providers/RouteServiceProvider.php,按照this change 5.2.27

以前,您需要到web中間件明確應用路線,像這樣:

Route::group(['middleware' => ['web']], function() { 
    Route::get('/', function() { 
     return view('welcome'); 
    }); 
}); 

以上,現在是實現如下:

Route::get('/', function() { 
    return view('welcome'); 
});