未定义的变量:yii2 kartik 网格中的 dataProvider

Undefined variable: dataProvider in yii2 kartik grid

我正在尝试了解 kartik 网格视图,但我在使用 dataProvider 时遇到了问题。

控制器代码

这是完整的控制器代码(尽管不是全部,我只显示代码直到包含我问过它为什么未定义的 dataProvider 的代码)

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

  }

查看代码

<?php echo GridView::widget([ 
'dataProvider' => $dataProvider,
'showOnEmpty' => true,
'emptyCell' => true,
'column' => [
    'username',
    'password',
    'akses',
    'kode_daerah',
    'authKey',
    'id',
    ],
'pjax' => true,
]);   ?>

输出为:

Undefined variable: dataProvider

错误: undefined var : dataProvider

more

more

more

求推荐。

问题已解决,我将第 65 行更改为 return $this->redirect(['site/knpr-home']);

并删除了视图站点中的所有列变量

<div>
<?php echo GridView::widget([ 
'dataProvider' => $dataProvider,
'showOnEmpty' => true,
'emptyCell' => true,

'pjax' => true,
]); ?> </div>