Search code examples
yii2yii2-model

yii2 create forms in main.php layout how to spend the $model


Hello everyone I am a beginner in yii2 , I have to create a form in the footer at the main.php layout section. Default does not pass the model . Could you give me an exemplary of how to pass data in the form ? Thanks so much.

Example of Controller and View.


Solution

  • For a single controller action a simpel way is use the params .

    In you controller action before render your view you should add

    public function actionYourAction()
    {
       .....
       $this->view->params['model'] = $model;
       .....
       $this->render(...); 
    
    }
    

    and for accessing in layout you should retrieve using

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