You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel中用Inertia提交表单后重定向至Blade页面异常(弹窗打开)

Laravel混合Inertia与Blade时重定向Blade页面异常问题解决

在Laravel应用中混合使用Inertia和Blade页面时,通过Inertia提交表单保存数据后,期望重定向到Blade视图,虽然能跳转到正确的页面,但页面却在模态框中打开。即使硬编码重定向地址,问题依然存在,怀疑除添加Inertia中间件外还有遗漏。

相关代码示例

userForm.vue

store () {
   this.form.post(this.route('user.store', {
      user: this.userData...
   }))
}

路由配置

Route::post('/{user}/store/', [UserController::class, 'store'])->name('user.store')->middleware('inertia');

UserController

return redirect('user/1');

解决方案

将控制器中的redirect替换为Inertia::location()方法,代码如下:

Inertia::location('/user/1');

原因是:当通过Inertia发起请求时,默认的redirect会被Inertia前端处理成单页应用内的导航,导致目标Blade页面被加载在当前的模态框上下文里。而Inertia::location()会触发浏览器的原生重定向,让页面在新的上下文(非模态框)中打开,从而解决问题。

内容的提问来源于stack exchange,提问作者jkstallard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 19:40:28