The original translation from: http://forumsarchive.laravel.io/viewtopic.php?id=10653, when there is time to translate a bit ~
This post would try to outline how to use the Redirect::intended and Redirect::guest methods.
There seems to bes a lot of confusion on the internet, along with a lot of custom solution in order to address this problem :
Unauthenticated user accesses ' auth ' filtered route
Filter stores the intended URL and redirects to the ' login ' route
User tries to authenticate (post to login route)
Login route authenticates the user, checks the session for a intended route, then redirects to if it exists, OTHERWI Se Redirect to a fallback URL.
L4 introduced the redirect::intended and Redirect::guest methods to solve this.
The relevant source is located in \vendor\laravel\framework\src\illuminate\routing\redirector.php.
/** * 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);}/** * 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->get(‘url.intended‘, $default);$this->session->forget(‘url.intended‘);return $this->to($path, $status, $headers, $secure);}
I believe it is used as follows (there could be some quirks, it working for me in my project-feel free to Contribu Te suggestions/changes)
Create a route protected by the ' auth ' filterroute::get (' protected ', Array (' before ' = ' auth ', function () {RET Urn view::make (' protected '); });/** the protected ' auth ' filter-this runs before the protected route* if the user is not authenticated-perform a Guest Redirect (this stores the intended URL is the session) */route::filter (' auth ', function () {//the user hasnt lo Gged in Yetif (Auth::guest ()) {//redirect them to the login page with the guest Method-this stores tended URL//Illuminate\routing\redirector.php-line Bayi: $this->session->put (' url.intended ', $this- >generator->full ()); return redirect::guest (' login ');}); * * Our basic login function-point a form to POST here* if the authentication is successful, we'll get the intended URL From the session and redirect to it.* if there is no intended URL (i.e. the user just navigated straight to the Unportec Ted login page), fallback to the specified route.* ifAuthentication is unsuccessful, you can do whatever-here we redirect to the login Page*/route::p ost (' Login ', function () { Get the credentials from input$creds = Array (' username ' =>input::get (' username '), ' Password ' =>input::get (' PA ssWOrd ')//successful login Attempt-redirect to the intended page, fallback to the '/' route if no inte Nded Pageif (Auth::attempt ($creds, True)) {return redirect::intended ('/');} Unsuccessful Login-redirect somewhereelse{return redirect::to (' login ');});
If anyone else wants to confirm this I'll write up a post with the code samples maybe to outline it more clearly, or make A PR for the docs, as the redirect::guest part was not in the documentation
Relationship and usage of redirect::intended and redirect::guest in Laravel4