Laravel 5.2会话闪存即使与Web中间件也不兼容 [英] Laravel 5.2 Session flash not working even with web middleware

查看:167
本文介绍了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']]

这篇关于Laravel 5.2会话闪存即使与Web中间件也不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆