Is It Wrong To Instantiate A Model In View?

I have my sign up and sign in forms in my mainlayout.php file.

so I have to instantiate them in the view right before I begin the form widget.

it works just fine but I know that the right way to instantiate the model is in the controller and passing it to the view.

so the question is, what is wrong with instantiating a model in view for my case?

is the like a security concern or something?

thank you

In my opinion I try to avoid it and instantiate in controller

But according to

Models can be used into views directly, so in special cases you can use it as you considered it.

But could you give more details what you want to do? I am sure there is a way to do it through controller…

About the security:

No, there is no risk if you have the same logic of the controller.

Also the views are inside protected folder (except if you use it in your theme folder…)

tanx for replying man.

I need to instantiate these two models in the view cause there is no controller for mainlayout.php.

so basically this is the only way to create form tied to models.

anyway I just wanted to make sure that there is no security issue with this.

SO I guess it’s Ok to instantiate the model in the view if there is no other way to do that.


I have few projects that I did it in the same way (widgets that run directly in layout and are displayed in any controller/action)

So keep going :)

Also you could override beforeAction in the base protected/component/controller.php, load the model that you want (it works in every request) and use it in your layout

I guess I could but it’s a lot easier to just instantiate the model.

anyway tanx a lot for helping me out.

It depends,

Initializing works even the model is new

afterFind works for stored model

So, according what you want to do you could choose one of them