views:

127

answers:

5

I am using Codeigniter to develop an application, and have been reading bits about Ruby on the Rails. In particular Skinny Controller, Fat Model seemed to make perfect sense. It really allows me to look at the controller and know what the hell is actually going on, and allows very very quick adjustments and bug fixes.

However the one stumbling block I have is, where do I handle POST data. Do I call it into a bunch of variables in the Controller and pass that to the model. Or do I just access it direct from the model?

Additionally I am having the same problem when thinking about prepopulating form field values. Should I call a single method from the controller and pass the result to the view or just make a bunch of variables and pass them to the view directly from the controller.

Any guidance greatly appreciated.

A: 

http://teknoid.wordpress.com/2009/01/06/another-way-to-think-about-mvc/

Looks like Models (bankers) should never talk direct to View (customer) and vice versa. So always use the Controller (teller) as middle-man.

esryl
+1  A: 

I'm not sure what the "rule" is here, but I usually stick to only checking what needs to be done in the controller. So find out what has been posted, and what should happen next. Usually a call to the model for a select, update, etc. including the POST data which gets processed further it in the model, depending on the called function.

As for pre-populating, I always try to keep all that logic out of the view. So in the controller I check which variables should be displayed (either default stuff or the POSTed data), and then send those to the view. That way the view stays simple, and the only thing you need to do is display a variable.

Alec
+1  A: 

There are a couple ways to handle this, but generally, it is not considered consistent for the view to call $this->CI->input->post( $item ). Generally, your view is best if restricted to purely displaying data and the only function calls it should make are calls like anchor, lang, et al. It is also better to avoid having your models grab data from outside, especially when they can be passed in the information (possible exceptions are language and config data). The traditional separation of concerns in CodeIgniter is that the Model stores data, the View displays data, and the Controller manipulates it (sometimes this will be libraries manipulate the data and the Controller calls library functions).

Pre-populating is clearer if the information is passed in to the view and then output from the view as <?php echo $value; ?>, but there are some exceptions.

Basically, these are the rules I follow:

  • The model receives no input save from the Controller.
    • It calls only helper functions and library methods if manipulation is needed.
    • It only manipulates data if the data needs to be stored in a particular way (ie: serialization, etc.).
  • The view receives no input save from the Controller.
    • It calls only helper functions for string lookups and no library methods.
    • The only time it needs to manipulates data is when it is looking up config or language data. This should be understood as translation.

Christopher W. Allen-Poole
+1  A: 

The controller should do all the data handling.

Use your controller to grab your POST data then pass it to the model.

The model confirms all went ok by passing true back to the controller.

At this point the controller passes the data to show to the success view.

Jon Winstanley
This is what I was looking for. However can you elaborate on how to use this idealogy for sending the options for a form select?
esryl
A: 

CodeIgniter has a wonderful form_validation library that runs in the controller: http://codeigniter.com/user_guide/libraries/form_validation.html#overview

After $_POST data has been quickly & easily validated by the form_validation library, according to your pre-set rules, you pass the validated data to the model and/or the view.

Summer
hi summer. yes using this, but i wanted to know am i calling it into an array in the controller before passing to model, or do i talk to $this->input->post() from the model.
esryl