how to pass parameter on redirect in yii

前端 未结 4 1993
鱼传尺愫
鱼传尺愫 2021-02-06 23:34

I am using Yii framework for my project;

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

$this->redir

4条回答
  •  旧时难觅i
    2021-02-06 23:52

    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.

提交回复
热议问题