码迷,mamicode.com
首页 > 其他好文 > 详细

laravel4 中 Redirect::intended和Redirect::guest的关系及用法

时间:2015-04-16 17:27:09      阅读:8270      评论:0      收藏:0      [点我收藏+]

标签:

原文转自:http://forumsarchive.laravel.io/viewtopic.php?id=10653,有空的时候再翻译一下~

This post will try to outline how to use the Redirect::intended and Redirect::guest methods.

There seems to be 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 an intended route, then redirects to that if it exists, otherwise 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 may be some quirks, but its working for me in my project - feel free to contribute suggestions/changes)

// Create a route protected by the ‘auth‘ filter
Route::get(‘protected‘, array(‘before‘=>‘auth‘, function()
{ 
        return 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 logged in yet
	if(Auth::guest()){
                // redirect them to the login page with the guest method - this stores the intended URL
                // Illuminate\Routing\Redirector.php - Line 81: $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 will get the intended url from the session and redirect to it.
* if there was no intended url (i.e. the user just navigated straight to the unportected login page), fallback to the specified route.
* if authentication is unsuccessful, you can do whatever - here we redirect to the login page
*/
Route::post(‘login‘, function()
{
        //get the credentials from input
	$creds = array(‘username‘=>Input::get(‘username‘),‘password‘=>Input::get(‘password‘))
        
        // successful login attempt - redirect to the intended page, fallback to the ‘/‘ route if no intended page
	if(Auth::attempt($creds,true)){
		return Redirect::intended(‘/‘);
	}
       // unsuccessful login - redirect somewhere
	else{
		return Redirect::to(‘login‘);
	}
});

if anyone else wants to confirm this i will write up a post in the code samples maybe to outline it more clearly, or make a PR for the docs, as the Redirect::guest part is not in the documentation

laravel4 中 Redirect::intended和Redirect::guest的关系及用法

标签:

原文地址:http://www.cnblogs.com/ZJAJS/p/4432427.html

(1)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!