How to use models from models/ in layouts/main.php

I have started to learn about Yii framework and I came up with the problem.

I have downloaded basic application template (so that you know the structure of my application).

I would like to use one of my classes’s function from models/ in main.php file from views/layouts/, however, I am not sure how I can access functions from models/. I have searched around the internet and none of the solutions have helped me.

  1. I have read about widjets, but was confused by the tutorial, because it did not talk about my version of the Yii.

  2. Also read some other solutions, but they did not work with me.

What would you suggest me to do? Because I am currently have no clue about the solution. I am a newbiew in this framework, so, don’t judge me hard :)

I am using: "Yii 2 with basic application template" form official Yii website.


Have you read the guide already?

To your question…

For example in main.php

// use statement on top of main.php like the other use statements

use app\models\Modelname; 

// anywhere in the file


$myModel = new Modelname; 



// Or if it is a static function: 



Yes, I have read the documentation and I was doing the same as you and I guess I was missing I piece of the information that did not allow me to make it work properly :D I am still reading it, and there is a lot more to learn about

Thank you! This helped me!