# Laravel 的 HTTP 重定向 Redirect
- [創建重定向](#creating-redirects)
- [重定向到命名路由](#redirecting-named-routes)
- [重定向到控制器動作](#redirecting-controller-actions)
- [閃存 Session 數據重定向](#redirecting-with-flashed-session-data)
<a name="creating-redirects"></a>
## 創建重定向
重定向響應是類 `Illuminate\Http\RedirectResponse` 的實例, 包含了重定向用戶到其他 URL 所需要的合適頭信息。有很多方式生成 `RedirectResponse` 實例。最簡單的方法是使用全局的 `redirect` 輔助函數:
Route::get('dashboard', function () {
return redirect('home/dashboard');
});
有時候你希望將用戶重定向到他們的上一個訪問位置,例如當提交的表單不合法時,你就可以通過全局的 `back` 輔助函數來這樣做。 因為該特性使用了 [session](/docs/{{version}}/session),請確保路由調用 `back` 函數時使用了 `web` 中間件組或者應用了全部的 session 中間件:
Route::post('user/profile', function () {
// Validate the request...
return back()->withInput();
});
<a name="redirecting-named-routes"></a>
## 重定向到命名路由
當你不帶參數調用 `redirect` 輔助函數時,會返回一個 `Illuminate\Routing\Redirector` 實例,它允許你調用 `Redirector` 實例上的任何方法。例如,你可以這樣使用 `route` 方法為命名路由生成一個 `RedirectResponse` :
return redirect()->route('login');
如果你的路由包含參數,你可以把它們當做第二參數傳給 `route` 方法:
// For a route with the following URI: profile/{id}
return redirect()->route('profile', ['id' => 1]);
#### 通過 Eloquent 模型填充參數
如果你要攜帶一個從 Eloquent 模型中填充過來的 "ID" 參數進行重定向,你可以簡單的把該模型本身傳進去。 ID 會被自動解析:
// For a route with the following URI: profile/{id}
return redirect()->route('profile', [$user]);
如果你想自定義路由參數的值,你應該在 Eloquent 模型中重寫 `getRouteKey` 方法:
/**
* Get the value of the model's route key.
*
* @return mixed
*/
public function getRouteKey()
{
return $this->slug;
}
<a name="redirecting-controller-actions"></a>
## 重定向到控制器動作
你也可以生成重定向到 [控制器動作](/docs/{{version}}/controllers)。要達到這個目的,傳遞控制器名和動作名到 `action` 方法即可。記住,你不需要指定完整的控制器命名空間,因為 Laravel 的 `RouteServiceProvider` 會自動設置基礎的控制器命名空間:
return redirect()->action('HomeController@index');
如果你的控制器路由需要參數,你可以把它們當做第二個參數傳遞給 `action` 方法:
return redirect()->action(
'UserController@profile', ['id' => 1]
);
<a name="redirecting-with-flashed-session-data"></a>
## 閃存 Session 數據重定向
重定向到新的 URL 并且 [閃存數據到 session](/docs/{{version}}/session#flash-data) 常常在同時完成。 通常的,這會在你成功的執行一個動作、閃存消息到 session 后完成。方便起見,你可以創建一個 `RedirectResponse` 實例并在單個的、流暢的方法鏈上閃存數據到 session :
Route::post('user/profile', function () {
// Update the user's profile...
return redirect('dashboard')->with('status', 'Profile updated!');
});
用戶被重定向后,你可以從 [session](/docs/{{version}}/session) 中顯示閃存消息。例如,使用 [Blade 語法](/docs/{{version}}/blade):
@if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
@endif
## 譯者署名
| 用戶名 | 頭像 | 職能 | 簽名 |
|---|---|---|---|
| [@limxx](https://github.com/limxx) | <img class="avatar-66 rm-style" src="https://avatars0.githubusercontent.com/u/16585030?v=4&s=400"> | 翻譯 | Winter is coming. |
---
> {note} 歡迎任何形式的轉載,但請務必注明出處,尊重他人勞動共創開源社區。
>
> 轉載請注明:本文檔由 Laravel China 社區 [laravel-china.org](https://laravel-china.org) 組織翻譯,詳見 [翻譯召集帖](https://laravel-china.org/topics/5756/laravel-55-document-translation-call-come-and-join-the-translation)。
>
> 文檔永久地址: https://d.laravel-china.org
- 說明
- 翻譯說明
- 發行說明
- 升級說明
- 貢獻導引
- 入門指南
- 安裝
- 配置信息
- 文件夾結構
- HomeStead
- Valet
- 核心架構
- 請求周期
- 服務容器
- 服務提供者
- 門面(Facades)
- Contracts
- 基礎功能
- 路由
- 中間件
- CSRF 保護
- 控制器
- 請求
- 響應
- 視圖
- 重定向
- Session
- 表單驗證
- 錯誤與日志
- 前端開發
- Blade 模板
- 本地化
- 前端指南
- 編輯資源 Mix
- 安全
- 用戶認證
- API認證
- 用戶授權
- 加密解密
- 哈希
- 重置密碼
- 綜合話題
- Artisan 命令行
- 廣播系統
- 緩存系統
- 集合
- 事件系統
- 文件存儲
- 輔助函數
- 郵件發送
- 消息通知
- 擴展包開發
- 隊列
- 任務調度
- 數據庫
- 快速入門
- 查詢構造器
- 分頁
- 數據庫遷移
- 數據填充
- Redis
- Eloquent ORM
- 快速入門
- 模型關聯
- Eloquent 集合
- 修改器
- API 資源
- 序列化
- 測試
- 快速入門
- HTTP 測試
- 瀏覽器測試 Dusk
- 數據庫測試
- 測試模擬器
- 官方擴展包
- Cashier 交易工具包
- Envoy 部署工具
- Horizon
- Passport OAuth 認證
- Scout 全文搜索
- Socialite 社交化登錄
- 交流說明