Laravel 5.2重定向回成功消息 [英] Laravel 5.2 redirect back with success message

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

这篇关于Laravel 5.2重定向回成功消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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