由于 yii2 中的 if 语句而出现解析错误

所以我有这段代码:


<?= GridView::widget([

    'dataProvider' => $detProvider,

    //'filterModel' => $searchModel1,

    if ($detProvider !== null) {

       'columns' => [

        ['class' => 'yii\grid\SerialColumn'],


       // 'id',

        'first_name',

        'last_name',

        'email',

        /*'application_id',*/


        [

                'class' => 'yii\grid\ActionColumn',

                'template' => '{view}',

                'buttons' => [

                    'view' => function ($url, $model, $key) {

                        return Yii::$app->user->can('viewApplicant') ? Html::a(Html::tag('span', '', ['class' => "glyphicon glyphicon-eye-open"]), $url, ['title' => Yii::t('yii', 'View')]):"";

                    },

                    /*'edit' => function ($url, $model, $key) {

                        return Yii::$app->user->can('editApplicant') ? Html::a(Html::tag('span', '', ['class' => "glyphicon glyphicon-pencil"]), $url, ['title' => Yii::t('yii', 'Edit')]):"";

                    },

                    'delete' => function ($url, $model, $key) {

                        return  Yii::$app->user->can('deleteApplicant') ? Html::a(Html::tag('span', '', ['class' => "glyphicon glyphicon-trash"]), $url, ['title' => Yii::t('yii', 'Delete'), 'data' => ['method' => 'post', 'confirm' => Yii::t('app', 'Are you sure you want to delete this item?'), 'params' => ['id' => $model['id'], '_csrf' => Yii::$app->request->csrfToken]]]):"";

                    },*/

                    ],

                            'urlCreator' => function($action, $model, $key, $index) {

                    if ($action === 'view') {

                        $url = 'index.php?r=admin/applicant/view&id=' . $model['id'];

                        return $url;

                    }

                }


                    ],

    ], 

    }


]); ?>

我放置该 if 语句是因为我希望仅当 detProvider 不为 null 时才显示此特定表。但是我收到一个解析错误;


ParseError 语法错误,意外的“if”(T_IF),期待“]”


有人可以帮忙吗?我究竟做错了什么??我还是 yii2 和 php 的新手。


繁花不似锦
浏览 106回答 1
1回答

慕容森

如果您只想在 dataProvider 不为 null 时显示 gridview,那么您应该以这种方式检查&nbsp; &nbsp; <?php&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if ($detProvider !== null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo GridView::widget([&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'dataProvider' => $detProvider,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //'filterModel' => $searchModel1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'columns' => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ['class' => 'yii\grid\SerialColumn'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 'id',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'first_name',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'last_name',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'email',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /*'application_id',*/&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'class' => 'yii\grid\ActionColumn',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'template' => '{view}',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'buttons' => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'view' => function ($url, $model, $key) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Yii::$app->user->can('viewApplicant') ? Html::a(Html::tag('span', '', ['class' => "glyphicon glyphicon-eye-open"]), $url, ['title' => Yii::t('yii', 'View')]):"";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /*'edit' => function ($url, $model, $key) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Yii::$app->user->can('editApplicant') ? Html::a(Html::tag('span', '', ['class' => "glyphicon glyphicon-pencil"]), $url, ['title' => Yii::t('yii', 'Edit')]):"";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'delete' => function ($url, $model, $key) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; Yii::$app->user->can('deleteApplicant') ? Html::a(Html::tag('span', '', ['class' => "glyphicon glyphicon-trash"]), $url, ['title' => Yii::t('yii', 'Delete'), 'data' => ['method' => 'post', 'confirm' => Yii::t('app', 'Are you sure you want to delete this item?'), 'params' => ['id' => $model['id'], '_csrf' => Yii::$app->request->csrfToken]]]):"";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },*/&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'urlCreator' => function($action, $model, $key, $index) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($action === 'view') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $url = 'index.php?r=admin/applicant/view&id=' . $model['id'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $url;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ?>
打开App,查看更多内容
随时随地看视频慕课网APP