Home » Php » php – how to pass parameter on redirect in yii

php – how to pass parameter on redirect in yii

Posted by: admin April 23, 2020 Leave a comment

Questions:

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)

How to&Answers:

Try:

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

api yii 1, api yii 2

Answer:

try this:

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

Answer:

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.

Answer:

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

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