Commit 875e2c3a by Qiang Xue

Fixes #4395: Added `$checkAjax` parameter to `yii\web\Response::redirect()` to…

Fixes #4395: Added `$checkAjax` parameter to `yii\web\Response::redirect()` to support default redirection behavior for AJAX/PJAX requests
parent 97e48139
...@@ -50,6 +50,7 @@ Yii Framework 2 Change Log ...@@ -50,6 +50,7 @@ Yii Framework 2 Change Log
- Enh #4146: Added `yii\bootstrap\ButtonDropdown::$containerOptions` (samdark) - Enh #4146: Added `yii\bootstrap\ButtonDropdown::$containerOptions` (samdark)
- Enh #4181: Added `yii\bootstrap\Modal::$headerOptions` and `yii\bootstrap\Modal::$footerOptions` (tuxoff, samdark) - Enh #4181: Added `yii\bootstrap\Modal::$headerOptions` and `yii\bootstrap\Modal::$footerOptions` (tuxoff, samdark)
- Enh #4263: Added migration and SQL schema files for `yii\log\DbTarget` (samdark) - Enh #4263: Added migration and SQL schema files for `yii\log\DbTarget` (samdark)
- Enh #4395: Added `$checkAjax` parameter to `yii\web\Response::redirect()` to support default redirection behavior for AJAX/PJAX requests (qiangxue)
- Enh #4450: Added `yii\bootstrap\Nav::renderDropdown()` (qiangxue) - Enh #4450: Added `yii\bootstrap\Nav::renderDropdown()` (qiangxue)
- Enh #4457: Added support for using noscript for css files registered through asset bundles and Html helper (samdark) - Enh #4457: Added support for using noscript for css files registered through asset bundles and Html helper (samdark)
- Enh #4492: Support PostgreSQL-specific syntax for `QueryBuilder::alterColumn()` (qiangxue) - Enh #4492: Support PostgreSQL-specific syntax for `QueryBuilder::alterColumn()` (qiangxue)
......
...@@ -735,9 +735,13 @@ class Response extends \yii\base\Response ...@@ -735,9 +735,13 @@ class Response extends \yii\base\Response
* @param integer $statusCode the HTTP status code. Defaults to 302. * @param integer $statusCode the HTTP status code. Defaults to 302.
* 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
* @param boolean $checkAjax whether to specially handle AJAX (and PJAX) requests. Defaults to true,
* meaning if the current request is an AJAX or PJAX request, then calling this method will cause the browser
* to redirect to the given URL. If this is false, a `Location` header will be sent, which when received as
* an AJAX/PJAX response, may NOT cause browser redirection.
* @return static the response object itself * @return static the response object itself
*/ */
public function redirect($url, $statusCode = 302) public function redirect($url, $statusCode = 302, $checkAjax = true)
{ {
if (is_array($url) && isset($url[0])) { if (is_array($url) && isset($url[0])) {
// ensure the route is absolute // ensure the route is absolute
...@@ -748,13 +752,18 @@ class Response extends \yii\base\Response ...@@ -748,13 +752,18 @@ class Response extends \yii\base\Response
$url = Yii::$app->getRequest()->getHostInfo() . $url; $url = Yii::$app->getRequest()->getHostInfo() . $url;
} }
if (Yii::$app->getRequest()->getIsPjax()) { if ($checkAjax) {
$this->getHeaders()->set('X-Pjax-Url', $url); if (Yii::$app->getRequest()->getIsPjax()) {
} elseif (Yii::$app->getRequest()->getIsAjax()) { $this->getHeaders()->set('X-Pjax-Url', $url);
$this->getHeaders()->set('X-Redirect', $url); } elseif (Yii::$app->getRequest()->getIsAjax()) {
$this->getHeaders()->set('X-Redirect', $url);
} else {
$this->getHeaders()->set('Location', $url);
}
} else { } else {
$this->getHeaders()->set('Location', $url); $this->getHeaders()->set('Location', $url);
} }
$this->setStatusCode($statusCode); $this->setStatusCode($statusCode);
return $this; return $this;
......
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