Help with controllers and views output

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

Do you have BookSearch model?

What is displayed when you add


\yii\helpers\VarDumper::dump(dataProvider);die();

after


$dataProvider = $searchModel->search(Yii::$app->request->queryParams);

?

Hi Thank you for your help.

Yes I have a search model.

I have added your code in the controller as shown below but the same error shows up:





<?php


namespace frontend\controllers;


use Yii;

use frontend\models\Book;

use frontend\models\BookSearch;

use yii\web\Controller;

use yii\web\NotFoundHttpException;

use yii\filters\VerbFilter;


/**

 * BookController implements the CRUD actions for Book model.

 */

class BookController extends Controller

{

    public function behaviors()

    {

        return [

            'verbs' => [

                'class' => VerbFilter::className(),

                'actions' => [

                    'delete' => ['post'],

                ],

            ],

        ];

    }


    /**

     * Lists all Book models.

     * @return mixed

     */

    public function actionIndex()

    {

        $searchModel = new BookSearch();

        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);


        return $this->render('index', [

            'searchModel' => $searchModel,

            'dataProvider' => $dataProvider,

        ]);

    }


    /**

     * Displays a single Book model.

     * @param integer $id

     * @param integer $author_id

     * @return mixed

     */

    public function actionView($id, $author_id)

    {

        return $this->render('view', [

            'model' => $this->findModel($id, $author_id),

        ]);

    }


    /**

     * Creates a new Book model.

     * If creation is successful, the browser will be redirected to the 'view' page.

     * @return mixed

     */

    public function actionCreate()

    {

        $model = new Book();


        if ($model->load(Yii::$app->request->post()) && $model->save()) {

            return $this->redirect(['view', 'id' => $model->id, 'author_id' => $model->author_id]);

        } else {

            return $this->render('create', [

                'model' => $model,

            ]);

        }

    }


    /**

     * Updates an existing Book model.

     * If update is successful, the browser will be redirected to the 'view' page.

     * @param integer $id

     * @param integer $author_id

     * @return mixed

     */

    public function actionUpdate($id, $author_id)

    {

        $model = $this->findModel($id, $author_id);


        if ($model->load(Yii::$app->request->post()) && $model->save()) {

            return $this->redirect(['view', 'id' => $model->id, 'author_id' => $model->author_id]);

        } else {

            return $this->render('update', [

                'model' => $model,

            ]);

        }

    }


    /**

     * Deletes an existing Book model.

     * If deletion is successful, the browser will be redirected to the 'index' page.

     * @param integer $id

     * @param integer $author_id

     * @return mixed

     */

    public function actionDelete($id, $author_id)

    {

        $this->findModel($id, $author_id)->delete();


        return $this->redirect(['index']);

    }


    /**

     * Finds the Book model based on its primary key value.

     * If the model is not found, a 404 HTTP exception will be thrown.

     * @param integer $id

     * @param integer $author_id

     * @return Book the loaded model

     * @throws NotFoundHttpException if the model cannot be found

     */

    protected function findModel($id, $author_id)

    {

        if (($model = Book::findOne(['id' => $id, 'author_id' => $author_id])) !== null) {

            return $model;

        } else {

            throw new NotFoundHttpException('The requested page does not exist.');

        }

    }

    

    public function findBook($id, $author_id){

        $model = Book::findOne(['id' => $id, 'author_id' => $author_id]);

    }

    

            public function actionAbout()

    {

        $searchModel = new BookSearch();

        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

         \yii\helpers\VarDumper::dump(dataProvider);die();

        return $this->render('about', [

            'searchModel' => $searchModel,

            'dataProvider' => $dataProvider,

        ]);

    }

}



#######################Error########################




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\base\View.php – require('C:\xampp\htdocs\forum\frontend\v...')



Sorry, it should be


\yii\helpers\VarDumper::dump($dataProvider);die();

.

Anyway - the same error message indicates that this is not the view that your controller is referring to.

I’ve just noticed that error shows this path C:\xampp\htdocs\forum\frontend\views\site\about.php so the view is for the SiteController.