Published on

How to create listview page in Yii2

Authors
  • avatar
    Name
    Bal Singh
    Twitter

In Yii2 there no default listview page. So if you need to create list view page, then its not a difficult task. It is very easy. You need to do following steps.

Step 1

First of all create your list action like

public function actionList()  
    {  
        $dataProvider = new ActiveDataProvider([  
            'query' => YourModelName::find();,  
         ]);  
        return $this->render('list', [  
            'dataProvider' => $dataProvider  
        ]);  
    }

Step 2

Create you list.php page in your view directory

<?php  
use yii\widgets\ListView;  
?>  
<?= ListView::widget([  
    'dataProvider' => $dataProvider,  
    'itemView' => '_item',  
]); ?>

Step 3

Last step, create _item.php page in your view directory. This page will contain the design of your list row. You can get data like

<h1><?= $model->title; ?></h1>