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

原文转自: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

转载于:https://www.cnblogs.com/ZJAJS/p/4432427.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值