Laravel 5.2重定向回成功消息

问题描述:

我正在尝试将成功消息返回到我在laravel上的主页.

I'm trying to get a success message back to my home page on laravel.

return redirect()->back()->withSuccess('IT WORKS!');

由于某些原因,在运行此代码后,变量$ success无法获得任何值.

For some reason the variable $success doesn't get any value after running this code.

我用来显示成功消息的代码:

The code I'm using to display the succes message:

@if (!empty($success))
    <h1>{{$success}}</h1>
@endif

我已将主目录和新闻页面添加到route.php中的Web中间件组中,如下所示:

I have added the home and newsletter page to the web middleware group in routes.php like this:

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

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

    Route::post('/newsletter/subscribe','NewsletterController@subscribe');
});

有人知道为什么这似乎不起作用吗?

Does anyone have any idea why this doesn't seem to work?

您应从routes.php中删除web中间件.手动添加web中间件会导致会话,并在Laravel中请求相关问题5.2.27及更高版本.

You should remove web middleware from routes.php. Adding web middleware manually causes session and request related problems in Laravel 5.2.27 and higher.

如果仍然没有帮助(仍然保持routes.php而不使用Web中间件),则可以尝试一些不同的方法:

If it didn't help (still, keep routes.php without web middleware), you can try little bit different approach:

return redirect()->back()->with('message', 'IT WORKS!');

显示消息(如果存在)

@if(session()->has('message'))
    <div class="alert alert-success">
        {{ session()->get('message') }}
    </div>
@endif