I'm trying to learn about kartik grid view and I'm having trouble with dataProvider.
Controller Code
this is the full controller code (not all though, i just show the code until code containing the dataProvider that i asked why it's undefined)
class SiteController extends Controller {
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
public function actionIndex()
{
if (!Yii::$app->user->isGuest){
if (Yii::$app->user->identity->akses === '1'){
return $this->render('knpr/knpr-home');
} else if (Yii::$app->user->identity->akses === '2') {
return $this->render('prov/prov-home');
} else if (Yii::$app->user->identity->akses === '3') {
return $this->render('kabkot/kabkot-home');
} else if (Yii::$app->user->identity->akses === '4') {
return $this->render('multiregional/multiregional-home');
}
}
//return $this->render('home-knpr');
}
public function actionLogin()
{ $this->layout = 'main-login';
if (!Yii::$app->user->isGuest) {
return $this->goHome();
}
$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
return $this->goBack();
}
return $this->render('login', [
'model' => $model,
]);
}
public function actionLogout()
{
Yii::$app->user->logout();
return $this->redirect(['site/login'])->send();
}
public function actionKnprHome()
{
$totalCount = Yii::$app->db->createCommand('SELECT COUNT(*) FROM m_admin') -> queryScalar();
$dataProvider = new SqlDataProvider([
'db' => Yii::$app->db,
'sql' => 'SELECT * FROM m_admin',
'totalCount' => $totalCount,
'sort' => false,
'pagination' => [
'pageSize' => $count,
],
]);
return $this->render('knpr/knpr-home', array('dataProvider' => $dataProvider));
}
View Code
<?php echo GridView::widget([
'dataProvider' => $dataProvider,
'showOnEmpty' => true,
'emptyCell' => true,
'column' => [
'username',
'password',
'akses',
'kode_daerah',
'authKey',
'id',
],
'pjax' => true,
]); ?>
and the output is :
Undefined variable: dataProvider
error : undefined var : dataProvider
Please suggest.
Problem solved, i changed line 65 into return $this->redirect(['site/knpr-home']);
and deleted all of the column variable in view site
<div>
<?php echo GridView::widget([
'dataProvider' => $dataProvider,
'showOnEmpty' => true,
'emptyCell' => true,
'pjax' => true,
]); ?> </div>