This method adds a "Location" header to the current response. Note that it does not send out
the header until
Response::send is called. In a controller action you may use this method as follows:
php
return Yii::$app->getResponse()->redirect($url);
In other places, if you want to send out the "Location" header immediately, you should use
the following code:
php
Yii::$app->getResponse()->redirect($url)->send();
return;
In AJAX mode, this normally will not work as expected unless there are some
client-side JavaScript code handling the redirection. To help achieve this goal,
this method will send out a "X-Redirect" header instead of "Location".
If you use the "yii" JavaScript module, it will handle the AJAX redirection as
described above. Otherwise, you should write the following JavaScript code to
handle the redirection:
javascript
$document.ajaxComplete(function (event, xhr, settings) {
var url = xhr.getResponseHeader('X-Redirect');
if (url) {
window.location = url;
}
});