Search code examples
phpgridviewyii2yii-chtml

Class 'CHtml' not found in Gridview in Yii2


I am developing Yii2 application where I want to list orders data in GridView.

User can click on order id and will be redirected to another GridView with order details listing. I want to display link with order id to redirect to order details page.

But I got the following error:

Class 'CHtml' not found

in GridView. Here is my code:

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
         ['class' => 'yii\grid\SerialColumn'],
         [
             'attribute'  => 'id',
             'value' => CHtml::link("Link", Yii::app()->createUrl("user/view")),
             'format'  => 'raw',
         ],
         'user_id',
         'amount',
         'promo_code_used',   
         ['class' => 'yii\grid\ActionColumn'],
    ],
]) ?>

Solution

  • Thers is no CHtml in Yii2. Use Html helper instead (see docs here and here). For generating hyperlink use Html::a() instead of Chtml::link(). So the code will be like this:

    use yii\helpers\Html;    
    
    ...
    
    echo Html::a("Link", ["user/view"]);