Yii2 GridView 将列内容数据与其他控制器呈现的参数进行比较

Yii2 GridView comparing Column content data to other controller rendered param

我创建了一个 Yii2 Gridview 小部件,我想显示 "V" 仅在行上标记等于我从控制器传递的另一个渲染变量:

        <?=
             GridView::widget([
                               'dataProvider' => $dataProvider,
                               'filterModel' => $searchModel,
                                'columns' => [
                                        [
                                            'header' => 'Default',
                                            'content' => function ($model) {
                                              if ($model->id == $selected) {
                                                return Html::tag('i','',['class' => 'fa fa-check']);
                                              }
                                              return '';
                                            }
                                        ],

从控制器传递:

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

但是我收到一个错误:

Undefined variable: selected

我错过了什么?

匿名函数范围内不存在变量$selected。来自 php.net page for anonymous functions:

Closures may also inherit variables from the parent scope. Any such variables must be passed to the use language construct.

即:

'content' => function ($model) use ($selected) { ...