Commit 27ad7e1f by Qiang Xue

Added Controller::goHome().

parent 89fa7ed3
......@@ -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();
}
}
......@@ -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(
......
......@@ -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()
......
......@@ -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();
?>
......
......@@ -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.
......
......@@ -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;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment