From 27ad7e1fc7db0d958d97add09fac2fbf11077298 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Mon, 12 Aug 2013 13:19:37 -0400 Subject: [PATCH] Added Controller::goHome(). --- .../backend/controllers/SiteController.php | 4 ++-- .../frontend/controllers/SiteController.php | 10 ++++----- apps/basic/controllers/SiteController.php | 4 ++-- apps/basic/views/layouts/main.php | 3 ++- framework/yii/web/Controller.php | 24 ++++++++++++++++++---- framework/yii/web/Response.php | 20 ++++++++++++++---- 6 files changed, 47 insertions(+), 18 deletions(-) diff --git a/apps/advanced/backend/controllers/SiteController.php b/apps/advanced/backend/controllers/SiteController.php index 09052d2..480406a 100644 --- a/apps/advanced/backend/controllers/SiteController.php +++ b/apps/advanced/backend/controllers/SiteController.php @@ -17,7 +17,7 @@ class SiteController extends Controller { $model = new LoginForm(); if ($model->load($_POST) && $model->login()) { - return $this->redirect(array('site/index')); + return $this->goHome(); } else { return $this->render('login', array( 'model' => $model, @@ -28,6 +28,6 @@ class SiteController extends Controller public function actionLogout() { Yii::$app->user->logout(); - return $this->redirect(array('site/index')); + return $this->goHome(); } } diff --git a/apps/advanced/frontend/controllers/SiteController.php b/apps/advanced/frontend/controllers/SiteController.php index c4f7f53..0c1b2f5 100644 --- a/apps/advanced/frontend/controllers/SiteController.php +++ b/apps/advanced/frontend/controllers/SiteController.php @@ -30,7 +30,7 @@ class SiteController extends Controller { $model = new LoginForm(); if ($model->load($_POST) && $model->login()) { - return $this->redirect(array('site/index')); + return $this->goHome(); } else { return $this->render('login', array( 'model' => $model, @@ -41,7 +41,7 @@ class SiteController extends Controller public function actionLogout() { Yii::$app->user->logout(); - return $this->redirect(array('site/index')); + return $this->goHome(); } public function actionContact() @@ -68,7 +68,7 @@ class SiteController extends Controller $model->setScenario('signup'); if ($model->load($_POST) && $model->save()) { if (Yii::$app->getUser()->login($model)) { - $this->redirect('index'); + return $this->goHome(); } } @@ -84,7 +84,7 @@ class SiteController extends Controller if ($model->load($_POST) && $model->validate()) { if ($this->sendPasswordResetEmail($model->email)) { Yii::$app->getSession()->setFlash('success', 'Check your email for further instructions.'); - $this->redirect('index'); + return $this->goHome(); } else { Yii::$app->getSession()->setFlash('error', 'There was an error sending email.'); } @@ -108,7 +108,7 @@ class SiteController extends Controller $model->scenario = 'resetPassword'; if ($model->load($_POST) && $model->save()) { Yii::$app->getSession()->setFlash('success', 'New password was saved.'); - $this->redirect('index'); + return $this->goHome(); } return $this->render('resetPassword', array( diff --git a/apps/basic/controllers/SiteController.php b/apps/basic/controllers/SiteController.php index 0c73873..cd0b3fb 100644 --- a/apps/basic/controllers/SiteController.php +++ b/apps/basic/controllers/SiteController.php @@ -31,7 +31,7 @@ class SiteController extends Controller { $model = new LoginForm(); if ($model->load($_POST) && $model->login()) { - return $this->redirect(array('site/index')); + return $this->goHome(); } else { return $this->render('login', array( 'model' => $model, @@ -42,7 +42,7 @@ class SiteController extends Controller public function actionLogout() { Yii::$app->user->logout(); - return $this->redirect(array('site/index')); + return $this->goHome(); } public function actionContact() diff --git a/apps/basic/views/layouts/main.php b/apps/basic/views/layouts/main.php index 67b74af..03b09fc 100644 --- a/apps/basic/views/layouts/main.php +++ b/apps/basic/views/layouts/main.php @@ -37,7 +37,8 @@ app\config\AppAsset::register($this); Yii::$app->user->isGuest ? array('label' => 'Login', 'url' => array('/site/login')) : array('label' => 'Logout (' . Yii::$app->user->identity->username .')' , 'url' => array('/site/logout')), - ))); + ), + )); NavBar::end(); ?> diff --git a/framework/yii/web/Controller.php b/framework/yii/web/Controller.php index 137b630..b094981 100644 --- a/framework/yii/web/Controller.php +++ b/framework/yii/web/Controller.php @@ -96,14 +96,21 @@ class Controller extends \yii\base\Controller * Redirects the browser to the specified URL. * This method is a shortcut to [[Response::redirect()]]. * - * @param array|string $url the URL to be redirected to. [[Html::url()]] - * will be used to normalize the URL. If the resulting URL is still a relative URL - * (one without host info), the current request host info will be used. + * @param string|array $url the URL to be redirected to. This can be in one of the following formats: + * + * - a string representing a URL (e.g. "http://example.com") + * - a string representing a URL alias (e.g. "@example.com") + * - an array in the format of `array($route, ...name-value pairs...)` (e.g. `array('site/index', 'ref' => 1)`) + * [[Html::url()]] will be used to convert the array into a URL. + * + * Any relative URL will be converted into an absolute one by prepending it with the host info + * of the current request. + * * @param integer $statusCode the HTTP status code. If null, it will use 302 * for normal requests, and [[ajaxRedirectCode]] for AJAX requests. * See [[http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html]] * for details about HTTP status code - * @return Response the response object itself + * @return Response the current response object */ public function redirect($url, $statusCode = null) { @@ -111,6 +118,15 @@ class Controller extends \yii\base\Controller } /** + * Redirects the browser to the home page. + * @return Response the current response object + */ + public function goHome() + { + return Yii::$app->getResponse()->redirect(Yii::$app->getHomeUrl()); + } + + /** * Refreshes the current page. * This method is a shortcut to [[Response::refresh()]]. * @param string $anchor the anchor that should be appended to the redirection URL. diff --git a/framework/yii/web/Response.php b/framework/yii/web/Response.php index a9190b5..ac5fc77 100644 --- a/framework/yii/web/Response.php +++ b/framework/yii/web/Response.php @@ -563,9 +563,17 @@ class Response extends \yii\base\Response * return Yii::$app->getResponse()->redirect($url); * ~~~ * - * @param string $url the URL to be redirected to. This can be a URL or an alias of the URL. - * The URL can be either relative or absolute. If relative, the host info of the current request - * will be prepend to the URL. + * @param string|array $url the URL to be redirected to. This can be in one of the following formats: + * + * - a string representing a URL (e.g. "http://example.com") + * - a string representing a URL alias (e.g. "@example.com") + * - an array in the format of `array($route, ...name-value pairs...)` (e.g. `array('site/index', 'ref' => 1)`). + * Note that the route is with respect to the whole application, instead of relative to a controller or module. + * [[Html::url()]] will be used to convert the array into a URL. + * + * Any relative URL will be converted into an absolute one by prepending it with the host info + * of the current request. + * * @param integer $statusCode the HTTP status code. If null, it will use 302 * for normal requests, and [[ajaxRedirectCode]] for AJAX requests. * See [[http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html]] @@ -574,7 +582,11 @@ class Response extends \yii\base\Response */ public function redirect($url, $statusCode = null) { - $url = Yii::getAlias($url); + if (is_array($url) && isset($url[0])) { + // ensure the route is absolute + $url[0] = '/' . ltrim($url[0], '/'); + } + $url = Html::url($url); if (strpos($url, '/') === 0 && strpos($url, '//') !== 0) { $url = Yii::$app->getRequest()->getHostInfo() . $url; }