Yii如何用json响应重定向到绝对URL


Yii how to redirect to absolute URL with json response

我的控制器

function actionLogin()
{
     //After success validation
     $url = $_POST['returnUrl']; // www.example.com/get_response/
     $arr = array('response' => 'Failed', 'msg' => 'login success','tokenKey'=> 'token');
    //How to send the response to url
}

在我上面的代码中,我必须将$arr数组发送到$url绝对url。

我找到了一些解决方案,它在这里有效吗

Yii::$app->getResponse()->redirect('http://newdomain.com');

但我不知道如何在我的情况下使用,或者有更好的方法吗

在控制器中,使用:

$this->redirect("absolute URL");

如果您想重定向到特定操作:

$this->redirect(array('controller/action'));

我们可以使用getBaseUrl来获取绝对url

$this->redirect(Yii::app()->getRequest()->getBaseUrl(true));

在这里您可以阅读如何在Yii中重定向。

以下代码应该适用于您:

function actionLogin()
{
     //After success validation
     $url = $_POST['returnUrl']; // www.example.com/get_response/
     $arr = array('response' => 'Failed', 'msg' => 'login success', 'tokenKey' => 'token');
     //How to send the response to url
     $this->redirect('http://' . $url . '?' . http_build_query($arr));
}

您必须自己构建http查询。Yii不支持任何帮助您创建带有查询参数的出站url的功能。