當咱們請求一個通過權限控制的請求不經過時,會跳轉到一個地方請求權限,請求結束後須要跳轉回以前的頁面。好比咱們請求一個須要登陸的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