23

I want to save the last place that a user visited before he click onto "Edit" button in the gridview widget of a page. I created a variable named $lastAddress but I really dont know how to pass it onto the gridview and append it to the $url variable of "Edit" button. Can anyone show me how?

$lastAddress = 'xxx'; <?= GridView::widget([ ... [ 'class' => 'yii\grid\ActionColumn', 'template' => '{view} {update} {delete}', 'buttons' => [ 'update' => function ($url, $model) { $url .= '&lastAddress=' . $lastAddress; //This is where I want to append the $lastAddress variable. return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url); }, ], ], ], ]); ?> 
1
  • Please do not construct URLs like this. Use yii\helpers\Url instead. Commented Jan 23, 2017 at 12:35

1 Answer 1

68

Use use to pass in variables from the parent scope to a closure:

'update' => function ($url, $model) use ($lastAddress) { $url .= '&lastAddress=' . $lastAddress; //This is where I want to append the $lastAddress variable. return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url); }, 
Sign up to request clarification or add additional context in comments.

3 Comments

I have another one doubt related to the above question. From the above when ever the update action is perform, they assign one static value to $lastAddress and combine the $lastAddress value with $url. But I have two column in dataProvider like wise, "tracker_id" and "id". I shown tracker_id as a link in gridview column. Whenever I click the tracker_id the corresponding dynamic id need to pass to the controlleraction. Can anyone help to me? Thanks in advance.
@topher Is it possible to do the other way around... set $lastAddress from within the function
I need to do it the other way around too, once found a value, update variable with it...?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.