Laravel 8 重定向

2021-07-17 15:58 更新

重定向響應(yīng)是 Illuminate\Http\RedirectResponse 類的實(shí)例,并且包含用戶需要重定向至另一個(gè) URL 所需的頭信息。Laravel 提供了幾種方法用于生成 RedirectResponse 實(shí)例。其中最簡(jiǎn)單的方法是使用全局輔助函數(shù) redirect

Route::get('dashboard', function () {
    return redirect('home/dashboard');
}); 

有時(shí)候你可能希望將用戶重定向到之前的位置,比如提交的表單無(wú)效時(shí)。這時(shí)你可以使用全局輔助函數(shù) back 來(lái)執(zhí)行此操作。由于這個(gè)功能利用了 session,請(qǐng)確保調(diào)用 back 函數(shù)的路由使用 web 中間件組或所有 Session 中間件:

Route::post('user/profile', function () {
    // Validate the request...

    return back()->withInput();
}); 
Route::post('user/profile', function () {
    // Validate the request...

    return back()->withInput();
}); 
    


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)