How to use all conditions in the main.php layout file in yii2

Iam new to yii 2.0 I want to display all project names in layout file.

I can use the following code in layouts -> main.php

                <?php $model = Project_manage::find()->all(); ?>

      

When I use this code in the main .php

The following error will come:

Error (#1)

An internal server error occurred.(These Error are came)

      

my Bootstrap file hosts the following code

Yii::setAlias('common', dirname(__DIR__));
Yii::setAlias('frontend', dirname(dirname(__DIR__)) . '/frontend');
Yii::setAlias('backend', dirname(dirname(__DIR__)) . '/backend');
Yii::setAlias('console', dirname(dirname(__DIR__)) . '/console');

Event::on(View::className(), View::EVENT_BEFORE_RENDER, function() {
$model = Project_manage::find()->all();
Yii::$app->view->params['model'] = $model;
});

      

Placing the above code will display the following error:

 Fatal error: Class 'Event' not found in   D:\wamp\www\yii2\common\config\bootstrap.php on line 7

      

In yii 2.0 how to use find all condition in layout file

Please help me fix this,

Thank.

+3


source to share


1 answer


You can use EVENT_BEFORE_RENDER: put this in your normal \ config \ bootstrap.php

    use yii\base\Event;
    use yii\base\View;

    Event::on(View::className(), View::EVENT_BEFORE_RENDER, function() {

        $model = Project_manage::find()->all();
        Yii::$app->view->params['model'] = $model;

    });

      



Then in your main layout you can use your model like:

$model= $this->params['model'];

      

+2


source







All Articles