Skip to content

Redirect to History – Yii

Reading Time: < 1 minute

Yii is an open source, object-oriented, component-based MVC PHP web application framework. Yii is pronounced as “Yee” and in Chinese it means “simple and evolutionary”

Yii is the one of major MVC framework of PHP. We need to redirect the previous / history page for some circumstances in every development.

Here we show how to achieve that;

If you want to get previous URL use below line of code

echo Yii::app()->request->urlReferrer;

If you want to redirect to the previous URL use below line of code

$this->redirect(Yii::app()->request->urlReferrer);

 To redirect to absolute URL with json response

Following code should work for you:

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

You have to build your http query by your own. Yii does not support any function that helps you to create outbound urls with query parameters.

In controller, use:

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

If you want redirect to action:

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

To ensure route is working, you have to pass array to CController::redirect():

    // for example
    $this->redirect(['/route/starts/from/slash']);
    // your case
    $this->redirect(['/tradesman/default/index']);

In other case you will have redirect broken on url rules change.

See also  3D Mobile Game Development 🏈 - 8 Kickstart Skills To Start

Leave a Reply