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
konapaz
(Konapaz)
March 11, 2014, 3:04pm
2
In my opinion I try to avoid it and instantiate in controller
But according to
http://www.yiiframework.com/doc/guide/1.1/en/basics.mvc
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…
konapaz
(Konapaz)
March 11, 2014, 3:09pm
3
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…)
KonApaz:
In my opinion I try to avoid it and instantiate in controller
But according to
http://www.yiiframework.com/doc/guide/1.1/en/basics.mvc
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…
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.
konapaz
(Konapaz)
March 13, 2014, 11:09am
5
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.
Yes,
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
konapaz
(Konapaz)
March 13, 2014, 11:17am
6
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.
konapaz
(Konapaz)
March 19, 2014, 10:13am
8
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