![]() |
Help with the MVC principle. - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Help with the MVC principle. (/showthread.php?tid=28726) |
Help with the MVC principle. - El Forum - 03-19-2010 [eluser]silverback[/eluser] Hi, I'm new to CI (but I've gone through almost all of the net tuts series) and OOP. Anyway I'm excited to start using it, but honestly the MVC principle is a bit foreign to me. Are there any guides specifically devoted to the big-picture design of applications (so an example of how an application is divided into controllers, models, views and how they interact). Note that I do understand what they all do and how they work, and I'd be able to make a good application at this stage, I just want to make sure everything is in the right place and conforms to MVC principles. Thanks Help with the MVC principle. - El Forum - 03-19-2010 [eluser]danoph[/eluser] Hi silverback...I would recommend watching the intro tutorials at http://rubyonrails.org to learn more about why we use MVC. I think those videos do a good job of explaining why separating your code is good practice and necessary in the long run. It keeps your code easier to manage and organized. A brief explanation of MVC: Controllers are the first place that things happen in your website application. If someone visits http://www.yourwebsitehere.com, the controller is usually where things start. You should call your models and views from here, but try not to have database queries in your controller functions. That's what models are for. Models are "objects" and usually interact with your database. For example, if you have an online store like amazon, your models would probably consist of Products, Tags, Users, Carts, etc. The functions inside your models should handle all database queries so you can retrieve these "objects" easily from your controllers. For example, if you want to display featured products on your homepage, your controller should be able to call a function in your Products model like this: Code: $feaured_products = $this->products->find_featured(); Meanwhile, your "Products" model should handle all the database queries and variables in order to return your featured products. Views are what the end user sees. You should try to keep as little PHP code in your views as possible. If you need functions in your views, you should be putting them somewhere else such as in your controllers, models or helpers. There are a lot of good books and websites on MVC and I think the ones dedicated to ruby on rails offer the best explanation on how to use MVC and why it's so much better. Help with the MVC principle. - El Forum - 03-19-2010 [eluser]AlexJ[/eluser] Well, you can use the result_object() after your queries, and pass that to your views, then you can use a foreach loop to go over the objects and their properties. |