在官方文件:Views & Responses裡呼叫Redirect類別靜態函式,背後是在呼叫這支Redirector.php類別的實體。
主要功能是由UrlGenerator類別產生URL、再丟給RedirectResponse類別產生轉址的HTTP header。
此外Redirector還提供某些需要紀錄當前URL、上一個URL相關的轉址功能,所以還需要Session的協助。
此處提到的許多函式,官網都沒有寫出來喔!
generator = $generator; } /** * Create a new redirect response to the "home" route. * * @param int $status * @return \Illuminate\Http\RedirectResponse */ public function home($status = 302) { return $this->to($this->generator->route('home'), $status); }
如果有設定名為’home’的Named Route,這個函式可以直接轉往它。
所以Laravel其實鼓勵你的routes中設一個名為home的首頁呢。
/** * Create a new redirect response to the previous location. * * @param int $status * @param array $headers * @return \Illuminate\Http\RedirectResponse */ public function back($status = 302, $headers = array()) { $back = $this->generator->getRequest()->headers->get('referer'); return $this->createRedirect($back, $status, $headers); }
從HTTP HEADER的REFERER找到前一個網址,然後轉過去。
/** * Create a new redirect response to the current URI. * * @param int $status * @param array $headers * @return \Illuminate\Http\RedirectResponse */ public function refresh($status = 302, $headers = array()) { return $this->to($this->generator->getRequest()->path(), $status, $headers); }
轉往原網址。
咦???什麼情況會需要原地轉址???
/** * Create a new redirect response, while putting the current URL in the session. * * @param string $path * @param int $status * @param array $headers * @param bool $secure * @return \Illuminate\Http\RedirectResponse */ public function guest($path, $status = 302, $headers = array(), $secure = null) { $this->session->put('url.intended', $this->generator->full()); return $this->to($path, $status, $headers, $secure); }
轉址的同時記下當前的URL。
/** * Create a new redirect response to the previously intended location. * * @param string $default * @param int $status * @param array $headers * @param bool $secure * @return \Illuminate\Http\RedirectResponse */ public function intended($default = '/', $status = 302, $headers = array(), $secure = null) { $path = $this->session->pull('url.intended', $default); return $this->to($path, $status, $headers, $secure); }
轉往上一次guest函式執行時記下的URL。
/** * Create a new redirect response to the given path. * * @param string $path * @param int $status * @param array $headers * @param bool $secure * @return \Illuminate\Http\RedirectResponse */ public function to($path, $status = 302, $headers = array(), $secure = null) { $path = $this->generator->to($path, array(), $secure); return $this->createRedirect($path, $status, $headers); }
轉往指定URL。
能接受$secure參數,判斷是否為HTTPS協定,修改URL之後才轉。
/** * Create a new redirect response to an external URL (no validation). * * @param string $path * @param int $status * @param array $headers * @return \Illuminate\Http\RedirectResponse */ public function away($path, $status = 302, $headers = array()) { return $this->createRedirect($path, $status, $headers); }
轉往外部網址。
其實to函式也可以轉往外部網址,這個away函式目前沒有意義。
應該是放在這邊,提醒之後要實作此功能吧。
/** * Create a new redirect response to the given HTTPS path. * * @param string $path * @param int $status * @param array $headers * @return \Illuminate\Http\RedirectResponse */ public function secure($path, $status = 302, $headers = array()) { return $this->to($path, $status, $headers, true); }
轉往某HTTPS協定的網址。
意義不大,使用to函式並將第四個參數設為true即可。
/** * Create a new redirect response to a named route. * * @param string $route * @param array $parameters * @param int $status * @param array $headers * @return \Illuminate\Http\RedirectResponse */ public function route($route, $parameters = array(), $status = 302, $headers = array()) { $path = $this->generator->route($route, $parameters); return $this->to($path, $status, $headers); }
routes如果有命名的話,可以用此函式轉往特定name。
/** * Create a new redirect response to a controller action. * * @param string $action * @param array $parameters * @param int $status * @param array $headers * @return \Illuminate\Http\RedirectResponse */ public function action($action, $parameters = array(), $status = 302, $headers = array()) { $path = $this->generator->action($action, $parameters); return $this->to($path, $status, $headers); }
轉往某個controller action。就是類似’UserController@showProfile’的東西。
/** * Create a new redirect response. * * @param string $path * @param int $status * @param array $headers * @return \Illuminate\Http\RedirectResponse */ protected function createRedirect($path, $status, $headers) { $redirect = new RedirectResponse($path, $status, $headers); if (isset($this->session)) { $redirect->setSession($this->session); } $redirect->setRequest($this->generator->getRequest()); return $redirect; } /** * Get the URL generator instance. * * @return \Illuminate\Routing\UrlGenerator */ public function getUrlGenerator() { return $this->generator; } /** * Set the active session store. * * @param \Illuminate\Session\Store $session * @return void */ public function setSession(SessionStore $session) { $this->session = $session; } }