Welcome Guest, Not a member yet? Register   Sign In
where does the controller begin and the model end?
#1

[eluser]conspirisi[/eluser]
i've read some theory about mvc, you know how the model is the gatekeeper to the database etc, but are there any concrete tips to know where each one begins and ends?

Links to tutorials would be good. Also how applicable is rails mvc theory to codeigniter?
#2

[eluser]sophistry[/eluser]
i don't have an answer for you (right at the moment) except to say that this is a classic (and worthwhile) question. there are probably 20 threads on this topic.

please, when you find what you are looking for, add your insight and learning to the FAQ so that others may benefit.

oh, BTW, welcome to CI!
#3

[eluser]Michael Wales[/eluser]
The controller is for processing user input (whether it be a request for a URL, or submission of a form, an AJAX request, etc). The model is for processing information (retrieving data from a database, retrieving data from an XML file, parsing through a comma-delimited list of information, etc).
#4

[eluser]conspirisi[/eluser]
thanks guys,

some good tips, incidently i hope i didn't mention a dirty word there Smile (what trains runs on) as no one seems to have commented on it.
#5

[eluser]Michael Wales[/eluser]
haha - I skimmed over that one accident. Rails MVC theory doesn't directly apply to CI development but it can be very helpful. Check out my blog post: There IS Room For Both of You.
#6

[eluser]Crafter[/eluser]
It's best to think of your layers of the MVC framework as seperate, in other words, each is self contained and there is no beginning and end. However, the containment is limited to its function in the framework.

Theoretically, your model should be seperate from the controller. You might for example, want to move the storage off to some remore location where data is stored on some unusla media. This shouldn't matter to your controller.

In practice, however, we often find that the controller needs to know the structure of the model. For example, we often find this in controllers:
Code:
$data['field1'] = value1;
$data['field2'] = value2;

The only way we can make the controller truly independant from your data nodel is to introduce some kind of despatcher that feeds business rules and models to the requestor. Then it only makes sense to have a single generic controller and "model handler" within your application.
#7

[eluser]obiron2[/eluser]
The issue I always have is if you need to manipulate the raw data

for example, I have firstname, surname and an optional nickname all stored in the database. Because if other things I need to do, I have already retrieved these as object properties or array elements.

If I now need to create a display name which is "firstname 'nickname' surname" if the nickname exists or just "firstname surname" if it does not, should that manipulation be done in the controller or in the model, or even, should I pass it to the view...

personally, if I already have the data or functions in the model to get the raw data, I do the construction in the controller as it saves conditional coding in the view. If it is something that occurs on a regular basis then I would chuck it in a helper, but still call it from the controller.

obiron
#8

[eluser]danoph[/eluser]
I believe your model should be written once and updated only as needed. It should NOT manipulate data because:
1. If you use your model to just retrieve raw data, you can pass it on to the controller and have the controller manipulate the data, then passing it on to the view. Your model can be written once and have one purpose being the core of your project.
2. If you are testing new stuff on a LIVE website, you can create new methods in controllers to manipulate the data from the model without screwing up the website that people are viewing at that moment.
3. Having a model that you don't have to touch often helps separate your project and isolate problems, whether they are in the model, controller or views.

I wrote an article on this a while ago called How CodeIgniter Keeps You Organized
#9

[eluser]Crafter[/eluser]
Good stuff, danoph




Theme © iAndrew 2016 - Forum software by © MyBB