Commit d19446ba by Qiang Xue

Minor adjustment of Response::redirect()

parent 9d3c9db4
...@@ -498,20 +498,20 @@ class Response extends \yii\base\Response ...@@ -498,20 +498,20 @@ class Response extends \yii\base\Response
* @param array|string $url the URL to be redirected to. [[\yii\helpers\Html::url()]] * @param array|string $url the URL to be redirected to. [[\yii\helpers\Html::url()]]
* will be used to normalize the URL. If the resulting URL is still a relative 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. * (one without host info), the current request host info will be used.
* @param boolean $terminate whether to terminate the current application * @param integer $statusCode the HTTP status code. If null, it will use 302
* @param integer $statusCode the HTTP status code. Defaults to 302. * for normal requests, and [[ajaxRedirectCode]] for AJAX requests.
* See [[http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html]] * See [[http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html]]
* for details about HTTP status code. * for details about HTTP status code
* Note that if the request is an AJAX request, [[ajaxRedirectCode]] will be used instead. * @param boolean $terminate whether to terminate the current application
*/ */
public function redirect($url, $terminate = true, $statusCode = 302) public function redirect($url, $statusCode = null, $terminate = true)
{ {
$url = Html::url($url); $url = Html::url($url);
if (strpos($url, '/') === 0 && strpos($url, '//') !== 0) { if (strpos($url, '/') === 0 && strpos($url, '//') !== 0) {
$url = Yii::$app->getRequest()->getHostInfo() . $url; $url = Yii::$app->getRequest()->getHostInfo() . $url;
} }
if (Yii::$app->getRequest()->getIsAjax()) { if ($statusCode === null) {
$statusCode = $this->ajaxRedirectCode; $statusCode = Yii::$app->getRequest()->getIsAjax() ? $this->ajaxRedirectCode : 302;
} }
$this->getHeaders()->set('Location', $url); $this->getHeaders()->set('Location', $url);
$this->setStatusCode($statusCode); $this->setStatusCode($statusCode);
...@@ -615,7 +615,7 @@ class Response extends \yii\base\Response ...@@ -615,7 +615,7 @@ class Response extends \yii\base\Response
*/ */
public function getIsOk() public function getIsOk()
{ {
return 200 === $this->getStatusCode(); return $this->getStatusCode() == 200;
} }
/** /**
...@@ -623,7 +623,7 @@ class Response extends \yii\base\Response ...@@ -623,7 +623,7 @@ class Response extends \yii\base\Response
*/ */
public function getIsForbidden() public function getIsForbidden()
{ {
return 403 === $this->getStatusCode(); return $this->getStatusCode() == 403;
} }
/** /**
...@@ -631,7 +631,7 @@ class Response extends \yii\base\Response ...@@ -631,7 +631,7 @@ class Response extends \yii\base\Response
*/ */
public function getIsNotFound() public function getIsNotFound()
{ {
return 404 === $this->getStatusCode(); return $this->getStatusCode() == 404;
} }
/** /**
......
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