Wes
Wes

Reputation: 173

Is calling a model from a view in codeigniter bad?

Ok I have been creating a small test site using code igniter and so far I have my Login and logouts done. Those all follow proper MVC I think with logic in the controller, the actual work in the model and the presentation in the view. However I have come to a problem when doing stuff like including a news feed in the front-site, no login required. I was thinking of just putting a call in the Home view to the model responsible for displaying news feed stuff. Is this bad practice or not? Basically what is the best way for displaying stuff that is dynamic like a news feed or even a users photos when logged in?

Upvotes: 1

Views: 217

Answers (2)

tereško
tereško

Reputation: 58444

In correctly implemented MVC-inspired design pattern, the views are responsible for acquiring information from model layer and creating a response (made from multiple templates, if required). This means, that for views to request data from model layer is the correct thing to do.

What you should avoid is operation, that alter the state of model layer, because that part is responsibility of the controller.

Another thing that should be noted is that this applies only if you have real view instances. What CodeIgniter calls "views" are nothing but bunch of dumb templates. Templates should not request any data from anywhere.

Upvotes: 0

AlunR
AlunR

Reputation: 465

It's very wrong.

In an ideal world all your functions, data calls, manipulation, data inserts etc would be in the models.

The controller is used to decide which calls from the models are required and pass data back and forth. It's at this point that the controller can gather the information required for the views and pass them forward.

The only logic you should have in your views is if/else and for each loops. I 'prep' all my data before it goes to the view so date formatting, empty value handling etc is also done so my views are nothing more than plain HTML with a smattering of name; ?> type variables.

Upvotes: 1

Related Questions