CodeIgniter Forums
How to organise my Code using the MVC in CI - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: How to organise my Code using the MVC in CI (/showthread.php?tid=32207)



How to organise my Code using the MVC in CI - El Forum - 07-15-2010

[eluser]Namsu[/eluser]
I have been programming in PHP for at least 2 years and have a fair bit of experience. I have read the user guide for CI and want to start using it successfully in my applications. I understand the MVC approach quite well, however, I am wondering how some of you out there write your applications in CI. I was wondering if you could give me some tips for some of the following questions:

1) Although I understand the Controller is the main area, I could load all the library/helper/model etc in one file (controller class). But what is ideal when I am loading things like a form helper for the view file, placing the load statements in view or controller. I understand I could write it in the controller and it will be globally available in the correct view and model.

2) How to handle POST data? I can catch the form in a function yes. I could use the global $_POST or $_REQUEST array, which do you people use? Also perhaps considering I might want to catch it directly in a model.

3) Suppose I make a form in the view file. The form is submitted to the correct controller class via POST (http://www.site.co.uk/register/add). I validate the form, and I want my error and validation to appear in the view file. I would normally have a loop which output the array of errors found above the form. So do I do exactly the same as I did previously, use conditionals to determine if errors need to be displayed. I am trying to keep myself from over writing coding (which is why I came to this framework). I have seen some projects where the view has been loaded when errors found in the form, but not sure how it was plugged in with the original view file, maybe I am confused!

I have read through some Helper functions and Library classes not all. Also if you have any good example website projects written in CI it would be very helpful. I understand everyone has a different style in structuring their code. But just wanted to get best practice.


How to organise my Code using the MVC in CI - El Forum - 07-16-2010

[eluser]flaky[/eluser]
1) You should load it in the controller
2)
Code:
//e.g.
$this->input->post('user_name');
3)Use form validation library, here is an example
Code:
$this->form_validation->set_rules('user_name', 'Username', 'required');
if($this->form_validation->run()){
   //validation is successful
}
else {
   //validation failed
}