Class 'CHtml' not found in Gridview in Yii2

user1780370 picture user1780370 · May 15, 2015 · Viewed 13.2k times · Source

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

Answer

vitalik_74 picture vitalik_74 · May 15, 2015

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"]);