Yii2 在 dataGrid 视图中插入 if else 语句

Yii2 Insert if else statement inside the dataGrid view

我使用 yii2 框架创建了一个网站。我还为我的 crud 使用了 gii 生成器。现在我安装了一个扩展 kartik gridview。现在我想要在 kartikGridview 里面我想创建一个 if else 语句但是我得到了错误。是的,我可以做到,但在 kartik gridview 之外。有什么建议吗? 这是我的索引网格视图:

<?=  GridView::widget([
'id'=>'kv-grid-demo',
'dataProvider'=>$dataProvider,
'filterModel'=>$searchModel,
'columns'=> [
        // ['class' => 'yii\grid\SerialColumn'],
        // 'report_id',
        'reference_no',
        'subject',
            [
              'attribute' => 'doc_date',
              'filter'=>DatePicker::widget([
              'model' => $searchModel,
              'attribute' => 'doc_date',
              'removeButton' => false,
               'pluginOptions' => [
                    'autoclose'=>true,
                    'format' => 'yyyy-mm-dd',
                ],
                ])
            ],

            [
                'attribute' => 'for',
                'value' => 'namefor.fullName',
            ],
            [
                'attribute' => 'from',
                'value' => 'namefrom.fullName',
            ],
            [
            'attribute' => 'drawer_id',

            ],
            'user_id',
            'doc_name',
        // 'doc_file',
    [
    'class'    => 'yii\grid\ActionColumn',

    ],
    ],
'containerOptions'=>['style'=>'overflow: auto'], // only set when $responsive = false
'headerRowOptions'=>['class'=>'kartik-sheet-style'],
'filterRowOptions'=>['class'=>'kartik-sheet-style'],
'pjax'=>true, // pjax is set to always true for this demo
'panel' => [
'type' => GridView::TYPE_PRIMARY,
'heading' => '<h3 class="panel-title"><i class="fa fa-files-o"></i> Documents</h3>',
'responsive'=>true,
'hover'=>true,
// 'after' => true,
// 'showFooter' => true,
 'showFooter' => true,
],
// set your toolbar

'toolbar'=> [

    // Html::button('<i class="glyphicon glyphicon-plus"></i>', ['type'=>'button', 'title'=>Yii::t('kvgrid', 'Add Book'), 'class'=>'btn btn-success']) . ' '.
    '{export}',
    '{toggleData}',
],

// set export properties
'export'=>[
    'fontAwesome'=>true
],
'exportConfig' => [
     GridView::PDF => [
'filename' => 'Documents',
'config' => [
  'methods' => [
    'SetHeader' => [
      ['odd' => $pdfHeader, 'even' => $pdfHeader]
    ],
    'SetFooter' => [
      ['odd' => $pdfFooter, 'even' => $pdfFooter]
    ],
  ],
  'options' => [
    'title' => 'Documents',
    'subject' => 'Documents',
    'keywords' => 'pdf, Documents, export, other, keywords, here'
  ],
]

如何插入这个 if else 代码?

  <?php
     if(Yii::$app->user->identity->isUser()) 
     {
     }else{  ?>
            <p>
            <?=Html::a('<i class="fa fa-plus"></i>&nbsp;Create',['create'],['class' => 'btn btn-success']) ?>
            </p>
      <?php } ?>

到这一行?

'toolbar'=> [

    '{export}',
    '{toggleData}',
],

预先构造$toolbar数组:

$toolbar = [];
// Add dynamic values
if (!Yii::$app->user->identity->isUser()) {
    $toolbar[] = '<p>' . Html::a('<i class="fa fa-plus"></i>&nbsp;Create', ['create'], ['class' => 'btn btn-success']) . '</p>';
}
// Add permanent values
$toolbar = array_merge($toolbar, [
    '{export}',
    '{toggleData}',
]);

并将其传递给 GridView 中的 toolbar 选项:

<?= GridView::widget([
    // Other options
    'toolbar' => $toolbar,
    // Other options
]) ?>