Welcome Guest, Not a member yet? Register   Sign In
Help with the MVC principle.
#2

[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.


Messages In This Thread
Help with the MVC principle. - by El Forum - 03-19-2010, 07:41 AM
Help with the MVC principle. - by El Forum - 03-19-2010, 09:59 AM
Help with the MVC principle. - by El Forum - 03-19-2010, 03:42 PM



Theme © iAndrew 2016 - Forum software by © MyBB