Wetit Buahom
Wetit Buahom

Reputation: 17

Passing variable from controller to view page shows undefined variable in Yii2

I'm very new to Yii2 Framework. I want to pass variable from controller to view.

In controller:

public function actionDeleteAll(){
    $var1 = 'abc';
    return $this->redirect(array('index', 'var_view' => $var1 ));
}

In view:

<?php  
    echo $_GET['var_view'];
?>

But page show:

enter image description here

Upvotes: 0

Views: 983

Answers (2)

TomaszKane
TomaszKane

Reputation: 815

Use render() instead of redirect().

public function actionDeleteAll() {
    $var1 = 'abc';
    return $this->render('index', ['var_view' => $var1]);
}

Upvotes: 1

ScaisEdge
ScaisEdge

Reputation: 133400

if you really want a redirect to index .. passing the value in $var1 as var_view

public function actionDeleteAll(){
  $var1 = 'abc';
  return $this->redirect(array('index', 'var_view' => $var1 ));
}  

be sure that your index have a proper signature eg:

public function actionIndex($var_view)
{
     .... your code  .. 

    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
        'var_view' => $var_view 
    ]);
}

redirect work as function call so you must pass the proper param with corresponding param name

Upvotes: 1

Related Questions