frameworks - How to use models from models/ in layouts/main.php in yii 2.0 (basic template) -
i have started learn yii framework day ago , came problem.
i have downloaded basic application template (so know structure of application).
i use 1 of classes's function "models/" in "views/layouts/main.php", however, not sure how can access them "main.php". have searched around internet , none of solutions have helped me.
- i've read widget creation, can use in "main.php" in link - yii - how retrieve model data layout page?, not provide version of yii coulnt not find protected folder , etc. kind of confused.
- also read other solution, did not me well.
what suggest me do? because have no clue solution. newbiew in framework, so, don't judge me hard :)
updated: [solution]
// use statement on top of main.php other use statements use app\models\modelname; // anywhere in file .... $mymodel = new modelname; $mymodel->myfunction(); ... // or if static function: modelname::myfunction();
this how access model files (classes) folder models/ example views/layouts/main.php.
here deal - view "view". view should represent data, not data. don't want have logic in view.
anyway, if want show model - should pass model through controller, renders view.
controller like:
$model = new device; $this->render('index', array( 'model' => $model ));
then can use in view, data model.
you directly in view, not practice.
here can read basics models , best practices how use them.
update [yii 1.x]
use model-class in main.php, import @ place want use it:
yii::import('application.models.loginform');
after can use functions model class.
update 2
namespaces in yii2:
use yii\models\loginform;
at start of main.php layout-file. @ "layouts" part here.
Comments
Post a Comment