32
votes

I am using Yii framework for my project;

I am redirecting page after success of insertion in database to another controller using

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

During the redirection is it possible to pass any parameters just like in render,

$this->render('selectRefiner', array('param' => $data)

4
I don't understand why people doesn't refer to documentation. And yiis documentation is top notch unlike other frameworks. Just send it as array where 1st element is controller action and rest are key value pairs which you can get as GET variables. - itachi
@itachi for me yii documentation not very clear - Arthur Yakovlev

4 Answers

63
votes

Try:

$this->redirect(array('controller/action', 'param1'=>'value1', 'param2'=>'value2',...))

api yii 1, api yii 2

2
votes

try this:

Yii::$app->response->redirect(['site/dashboard','id' => 1, 'var1' => 'test']);
2
votes

You can only pass GET parameters in the Yii 2 redirect(). However, I had a similar situation and I resolved it by using Session storage.

Naturally, you can access current Session via Yii::$app->session. Here is an example of using it in two separate controller actions:

public function actionOne() {
    // Check if the Session is Open, and Open it if it isn't Open already
    if (!Yii::$app->session->getIsActive()) {
        Yii::$app->session->open();
    }
    Yii::$app->session['someParameter'] = 'Bool/String/Array...';
    Yii::$app->session->close();
    $this->redirect(['site/two']);
}

public function actionTwo() {
    if (isset(Yii::$app->session['someParameter']) {
       $param = Yii::$app->session['someParameter'];
    } else {
       $param = null;
    }
    $this->render('two', [
        'param' => $param
    ]);
}

So now you should be able to access $param inside the two view.

For more information, please refer to the official class documentation.

0
votes

To redirect into same action with the all parameters that already have this works for me.

$this->redirect($_PHP['SELF']);