Hello,
I am trying to play around with widgets as I am still new to Yii2, at the moment I am trying to output on a single page, database data passed from another controller, but I struggle.
I have a table called book, with id,author and title
I have created in my Book Controller a function:
public function actionAbout()
{
$searchModel = new BookSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('about', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
Now in my advanced template page called about.php, I have added this code:
<?php
use yii\helpers\Html;
use frontend\models\Book;
use yii\grid\GridView;
use yii\data\ArraydataProvider;
/* @var $this yii\web\View */
$this->title = 'About';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="site-about">
<h1><?= Html::encode($this->title) ?></h1>
<p>This is the About page. You may modify the following file to customize its content:</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'title',
'author',
'author_id',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<p>
</p>
<code><?= __FILE__ ?></code>
</div>
Unfortunately when I try to access the "about" page, I get:
PHP Notice – yii\base\ErrorException
Undefined variable: dataProvider
1. in C:\xampp\htdocs\forum\frontend\views\site\about.php at line 17
891011121314151617181920212223242526$this->title = 'About';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="site-about">
<h1><?= Html::encode($this->title) ?></h1>
<p>This is the About page. You may modify the following file to customize its content:</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'title',
'author',
'author_id',
2. in C:\xampp\htdocs\forum\frontend\views\site\about.php – yii\base\ErrorHandler::handleError(8, 'Undefined variable: dataProvider', 'C:\xampp\htdocs\forum\frontend\v...', 17, ...) at line 17
11121314151617181920212223<div class="site-about">
<h1><?= Html::encode($this->title) ?></h1>
<p>This is the About page. You may modify the following file to customize its content:</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'title',
3. in C:\xampp\htdocs\forum\vendor\yiisoft\yii2\ba
Any idea why please?
Thank you,
Ben