views:

59

answers:

3

I know normally the data is passed thru to the view with the controller. however, currently in my view I load my model ($this->load->model('Db_model');) so i can use it in a loop to retrieve a users profile picture path from a array of IDs that is passed from controller. Will loading the db model in the view to accomplish this make my site more vulnerable or bad form? To me it seems to be outside of MVC concept but its working atm. thanks

+3  A: 

Well if you do something outside of MVC it doesn't mean that it will stop working the very same second. MVC is just a design pattern which should help you design and maintain your site. The basic principle is that model should communicate only with controller and view also only with controller so your idea of calling model directly from the view is not MVC way of doing things.

If you need some additional data from the model why don't fetch it in controller and pass it as another parameter to the view so it can easily use it? It will be the same amount of code probably and your code will be much cleaner. Keeping your code clean may not seem like such a big deal right know when you remember where everything is stored but in few months when you forget some of this stuff you may end up with a headache if you mess your app too much.

RaYell
+4  A: 

I agree, but it has everything to do with scale. If you are designing a tiny app MVC does not really matter because it is easy to oversee the whole application. However once an application starts to grow, or if you are building a bigger application MVC separation becomes more important.

For instance if you use models in your views, this implies the design team has to know about models. It may also hamper porting the views later on to another framework or swapping templates.

Gabor de Mooij
I believe technically if the model is used for read-only behavior then you may still considered it MVC. In fact that sounds like a good idea to me, the controller never needs know.
Natebot
A: 

Look into Libraries. You should consider creating a library class helper for displaying the profile picture. You can then have the library call the model. Then, in your view, you simply do:

<?php $this->profile_helper->display_picture(); ?>

where profile_helper is your library class and display_picture() is your class function to display the users profile.

craide