Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL in Yii2 GridView

I have this code:

<?php echo GridView::widget([
  'dataProvider' => $dataProvider,
  'filterModel' => $searchModel,
  'columns' => [
    ['class' => 'yii\grid\SerialColumn'],
    [
       'label' => 'bla',
       'format' => 'url',
       'value' => function ($data) {
            return Html::url('site/index');
       },
    ],
    ['class' => 'yii\grid\ActionColumn'],
  ],
]); ?>

In grid view text is being generated with URL address.

/academia-new/advanced/admin/site/index

URL is working fine, but how can I set a text for link?

like image 316
nsv Avatar asked Apr 10 '14 14:04

nsv


3 Answers

Use 'format' => 'raw' instead of 'format' => 'url'.

like image 194
Ajey Avatar answered Oct 21 '22 20:10

Ajey


I got the solution from Samdark, contributor of yii. need to use format=>'raw':

...    
'format' => 'raw',
     'value'=>function ($data) {
        return Html::a(Html::encode("View"),'site/index');
    },

need to use Html::encode() to ecape XSS

like image 27
nsv Avatar answered Oct 21 '22 19:10

nsv


solution:

<?=  GridView::widget([
       'dataProvider' => $dataProvider,
       'filterModel' => $searchModel,
       'columns' => [
       ['class' => 'yii\grid\SerialColumn'],
             [
             'label'=>'bla',
             'format' => 'raw',
             'value'=>function ($data) {
                        return Html::a(['site/index']);
                      },
             ],
     ['class' => 'yii\grid\ActionColumn'],
  ],
]); ?>
like image 10
yacel100 Avatar answered Oct 21 '22 21:10

yacel100