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

来源:互联网 发布:淘宝能寄到印尼吗 编辑:程序博客网 时间:2024/06/11 06:13

当我们请求一个经过权限控制的请求不通过时,会跳转到一个地方请求权限,请求结束后需要跳转回之前的页面。比如我们请求一个需要登录的action,会被跳转到login页面,我们希望登录成功后跳转到我们之前希望去的action页面。要实现这个,只需要在login之后,执行以下这句即可:

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

为什么呢?因为在请求一个需要登录的aciton的跳转到登录页面之前,yii会把当前请求的url存到user对象的returnUrl属性中,方便后面的跳转。有代码为证(来自Yii源码):

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

/** * 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方法

/** * 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'));}


0 0
原创粉丝点击