URL in Yii2 GridView

59,540

Solution 1

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

Solution 2

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

Solution 3

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'],
  ],
]); ?>

Solution 4

try

return Html::a('link_text','site/index');

https://github.com/yiisoft/yii2/blob/master/framework/helpers/BaseHtml.php

Solution 5

use raw format

<?php echo GridView::widget([
  'dataProvider' => $dataProvider,
  'filterModel' => $searchModel,
  'columns' => [
    ['class' => 'yii\grid\SerialColumn'],
    [
           'label'=>'url',
           'format' => 'raw',
       'value'=>function ($data) {
            return Html::a('there is your label',['site/index']);
        },
    ],
    ['class' => 'yii\grid\ActionColumn'],
],
]); ?>
Share:
59,540
Sarvar N
Author by

Sarvar N

Updated on July 09, 2022

Comments

  • Sarvar N
    Sarvar N almost 2 years

    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?

  • FrederikNS
    FrederikNS over 9 years
    Some intentation would really help the readability of your answer
  • Viktors
    Viktors over 9 years
    return Html::a(['site/index']) do not work in yii2 -> error, try return Html::a('link','site/index');
  • Uzumaki Naruto
    Uzumaki Naruto almost 9 years
    what if you want to send some data with it lets say an integer .. and how would to retrieve it in controller ??
  • Andreas Hinderberger
    Andreas Hinderberger over 7 years
    [ 'attribute' => 'invoice_id', 'format' => 'raw', 'value' => function ($model) { return Html::a($model->invoice_id, [ 'invoice/view', 'id' => $model->invoice_id ], ['target' => '_blank']); }, ],
  • Nico Haase
    Nico Haase almost 6 years
    That does not look like valid PHP code. Can you highlight the parts that are crucial to solve the original problem?