douqiao8032 2016-05-22 15:26
浏览 118
已采纳

Laravel 5.2重定向成功消息

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

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

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

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?

  • 写回答

7条回答 默认 最新

  • douqianxian7008 2016-05-22 15:30
    关注

    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.

    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!');
    

    Displaying message if it exists:

    @if(session()->has('message'))
        <div class="alert alert-success">
            {{ session()->get('message') }}
        </div>
    @endif
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog