Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blog in CodeIgniter : Where Does the Model start, and the Controller End?

I'm testing CodeIgniter, and trying to create a simple blog. The video tutorial on the CodeIgniter site is nice, but very incomplete. I'm not too familiar with the MVC structure, and I am wondering exactly how a model is used. For instance, I'm currently doing the "admin" portion of my blog, which allows you to create, delete, and modify entries. The view only contains xhtml, and the controller takes care of the rest. What should be in the model? Does everything database related occur in the model (i.e. inserts, updates, selects, etc.)?

like image 815
Manu Avatar asked Jun 25 '09 16:06

Manu


2 Answers

Depends who you ask.

Some people like to put as much as possible in the model (validation, data retrieval, etc), and have the controller just poke it to get the data it needs, which it then hands over to the view.

Think about it like this: if you have more than one controller accessing a single model, then shouldn't common things between them be in a common place (as long as that common thing actually has something to do with the model)?

like image 88
nilamo Avatar answered Oct 05 '22 11:10

nilamo


The Model should contain everything database related, and perform all of the basic CRUD operations (Create, Get, Update, Delete).

The Controller should handle all communication between the model and the view. So for example, if you have a form for adding a new post, you should have a view for that form, which is called from a controller. The Controller would check to see if anything has been submitted, and if something has, call the create/insert method from the Post Model.

like image 29
GSto Avatar answered Oct 05 '22 09:10

GSto