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
- Enh #4146: Added `yii\bootstrap\ButtonDropdown::$containerOptions` (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 #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 #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)
......
......@@ -735,9 +735,13 @@ class Response extends \yii\base\Response
* @param integer $statusCode the HTTP status code. Defaults to 302.
* See <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html>
* 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
*/
public function redirect($url, $statusCode = 302)
public function redirect($url, $statusCode = 302, $checkAjax = true)
{
if (is_array($url) && isset($url[0])) {
// ensure the route is absolute
......@@ -748,6 +752,7 @@ class Response extends \yii\base\Response
$url = Yii::$app->getRequest()->getHostInfo() . $url;
}
if ($checkAjax) {
if (Yii::$app->getRequest()->getIsPjax()) {
$this->getHeaders()->set('X-Pjax-Url', $url);
} elseif (Yii::$app->getRequest()->getIsAjax()) {
......@@ -755,6 +760,10 @@ class Response extends \yii\base\Response
} else {
$this->getHeaders()->set('Location', $url);
}
} else {
$this->getHeaders()->set('Location', $url);
}
$this->setStatusCode($statusCode);
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