yii中登陆后跳转回登陆前请求的页面

当咱们请求一个通过权限控制的请求不经过时,会跳转到一个地方请求权限,请求结束后须要跳转回以前的页面。好比咱们请求一个须要登陆的action,会被跳转到login页面,咱们但愿登陆成功后跳转到咱们以前但愿去的action页面。要实现这个,只须要在login以后,执行如下这句便可: php


Yii:app()->getRequest()-redirect(Yii::app()->user->getReturnUrl());


为何呢?由于在请求一个须要登陆的aciton的跳转到登陆页面以前,yii会把当前请求的url存到user对象的returnUrl属性中,方便后面的跳转。有代码为证(来自Yii源码): html

//先遭到CAccessControllFilter拦截,执行它的accessDenied方法 app


/**
 * Denies the access of the user.
 * This method is invoked when access check fails.
 * @param IWebUser $user the current user
 * @param string $message the error message to be displayed
 */
protected function accessDenied($user,$message)
{
	if($user->getIsGuest())
		$user->loginRequired();
	else
		throw new CHttpException(403,$message);
}


//而后执行CWebUser中的loginRequired方法 yii

/**
 * Redirects the user browser to the login page.
 * Before the redirection, the current URL (if it's not an AJAX url) will be
 * kept in {@link returnUrl} so that the user browser may be redirected back
 * to the current page after successful login. Make sure you set {@link loginUrl}
 * so that the user browser can be redirected to the specified login URL after
 * calling this method.
 * After calling this method, the current request processing will be terminated.
 */
public function loginRequired()
{
	$app=Yii::app();
	$request=$app->getRequest();

	if(!$request->getIsAjaxRequest())
		$this->setReturnUrl($request->getUrl());
	elseif(isset($this->loginRequiredAjaxResponse))
	{
		echo $this->loginRequiredAjaxResponse;
		Yii::app()->end();
	}

	if(($url=$this->loginUrl)!==null)
	{
		if(is_array($url))
		{
			$route=isset($url[0]) ? $url[0] : $app->defaultController;
			$url=$app->createUrl($route,array_splice($url,1));
		}
		$request->redirect($url);
	}
	else
		throw new CHttpException(403,Yii::t('yii','Login Required'));
}
文章来源: http://sudodev.cn/articles/147.html
相关文章
相关标签/搜索