Laravel - 当我按后退按钮时浏览器再次显示消息

Gab*_*iel 2 php session materialize laravel

每次我在应用程序中创建新类别时,它都会显示一条成功消息。这是我的代码:

return redirect('dashboard/categorias')->with('message', 'Categoria criada com sucesso!');
Run Code Online (Sandbox Code Playgroud)

我知道这没什么大不了的,但是当我创建一个新类别时,我转到另一个页面并按浏览器上的后退按钮,它会再次显示该消息。

这是我的观点:

@if(session('message'))
        <script>Materialize.toast('{{session('message')}}', 4000)</script>
        {{ Session::flush() }}

@endif
Run Code Online (Sandbox Code Playgroud)

应该怎么做才能不再显示此消息?

Sha*_*yal 6

你应该在 Laravel 中进行会话闪烁,一旦会话数据被读取,它就不再可用:

$request->session()->flash('message', 'Categoria criada com sucesso!');
Run Code Online (Sandbox Code Playgroud)

或者

Session::flash('message', 'Categoria criada com sucesso!');
Run Code Online (Sandbox Code Playgroud)

更新

您的问题似乎是页面缓存,请使用以下标头:

header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
Run Code Online (Sandbox Code Playgroud)

在发送您的回复之前。

您甚至可以创建一个中间件来不使用缓存,请在此处查看答案:

/sf/answers/2944017821/