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

时间:2014-04-17 05:59:57

标签: php yii yii-extensions

我的控制器

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');
  

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

3 个答案:

答案 0 :(得分:1)

在控制器中,使用:

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

如果您想重定向到操作:

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

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

Here您可以阅读如何在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的函数。