在我的控制器中,我尝试使用laravel 5中的Flash消息重定向.这一切都很有效.问题是无论我如何设置它,如果我离开然后通过使用浏览器的"后退按钮"返回,闪存消息总会重新出现.
所以我有一个用户列表,每个用户旁边都有一个删除按钮.当我单击删除按钮时,它调用我的控制器方法来销毁.我在那里做了一个有条件的条件,如果我想删除一个所有者,重定向返回错误.我尝试了以下不同的方式重定向回来,但是我最后解决了导航离开并通过浏览器的"后退按钮"返回后再次显示消息的问题.
1
return Redirect::route('users')->with('error_message', 'Some error msg');
Run Code Online (Sandbox Code Playgroud)
2
Session::flash('error_message', 'Some error msg');
return Redirect::to('users');
Run Code Online (Sandbox Code Playgroud)
在我看来,我这样捡起来:
@if (Session::has('error_message'))
{{ Session::get('error_message') }}
@endif
Run Code Online (Sandbox Code Playgroud)
这样做很好,我收到了消息.但是,例如,我单击用户列表中的用户转到详细信息页面并按下浏览器的"后退按钮",消息将再次闪烁.我不明白为什么它一直在闪烁那些数据,我的印象是它只闪了一次.
即使我试图在显示后立即清除它(如下图所示),也无所谓,它会一直重新出现?
{!! Session::forget('error_message') !!}
Run Code Online (Sandbox Code Playgroud)
第 1 步:使用以下命令创建一个中间件:
php artisan make:middleware PreventBackHistory
第2步:
用以下内容替换 preventBackHistory.php 的内容:
namespace App\Http\Middleware;
use Closure;
class PreventBackHistory {
/* Handle an incoming request.
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$response = $next($request);
return $response->header('Cache-Control','no-cache, no-store, max-age=0, must-revalidate')
->header('Pragma','no-cache')
->header('Expires','Sun, 02 Jan 1990 00:00:00 GMT');
}
}
Run Code Online (Sandbox Code Playgroud)
第三步:在kernal.php中注册中间件
'preventBackHistory' => \App\Http\Middleware\PreventBackHistory::class,
第 4 步:在控制器的构造函数中使用中间件
public function __construct()
{
$this->middleware('preventBackHistory');
$this->middleware('auth');
}
Run Code Online (Sandbox Code Playgroud)
很高兴去:)
| 归档时间: |
|
| 查看次数: |
4351 次 |
| 最近记录: |