Laravel 5.2会话闪存即使与Web中间件也不兼容
我正在尝试使用会话实现Flash消息传递,但是无法实现.
I am trying to implement flash messaging using sessions but am unable to do so.
在我的控制器中,我有:
In my controller I have:
public function store(Request $request) {
session()->flash('donald', 'duck');
session()->put('mickey', 'mouse');
return redirect()->action('CustomerController@index')->with('bugs', 'bunny');
}
但是当我在视图中检查会话变量时,只能看到session()->put('mickey', 'mouse')
中的值.
But when I check the session variables in the view, I can only see the values from session()->put('mickey', 'mouse')
.
会话:
{"_token":"F6DoffOFb17B36eEJQruxvPe0ra1CbyJiaooDn3F","_previous":{"url":"http:\/\/localhost\/customers\/create"},"flash":{"old":[],"new":[]},"mickey":"mouse"}
许多人由于没有Web中间件内的相关路由而遇到了此问题.我也确保这样做,但仍然无法正常工作.
A lot of people encountered this problem by not having the relevant routes inside the web middleware. I made sure to do this as well but it still wouldn't work.
在routes.php中:
In routes.php:
Route::group(['middleware' => ['web']], function () {
Route::get('/', function () {
return view('welcome');
});
Route::get('/customers', 'CustomerController@index');
Route::get('/customers/create', 'CustomerController@create');
Route::post('/customers', 'CustomerController@store');
});
在Kernel.php中:
In Kernel.php:
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
],
'api' => [
'throttle:60,1',
],
];
有人可以让我知道我在这里做错了什么吗?谢谢!
Can anyone let me know what I could be doing wrong here? Thanks!
通过替换
Route::group(['middleware' => ['web']], function () {
...
});
使用
Route::group(['middlewareGroups' => ['web']], function () {
...
});
当所有文档都建议我们使用['middleware' => ['web']]
No idea why this works though when all the documentation suggests that we use ['middleware' => ['web']]