Thursday, October 14, 2010

Kohana is MVC framework. In this lesson, we will see what exactly the MVC letters mean and how to they fit in our application.

MVC stands for Model View Controller. The Model-View-Controller concept is often used in in web development. It provides a convinient way to separate logic and interface.The Controller is the gateway to the application. Depending on the user input (url of the page or submitted data), the controler decides what operation should be performed and what output should be generated.

The Model is the place where data is processed. It is the workforce of the application and gets the things done.

The View is the presentation layer. It handles the output, which will be send back to the browser.

So the tipical workflow of an MVC application is:

User opens page -> the request hits the controller -> the controller decides what action should be performed -> the controller calls the model if necessary (loads data or performs action) -> the controller loads the view -> the view generates the page and sends it back to the browser

There are some basic rules of thumb when dealing with MVC:

Keep your controllers on a diet. The controller gets hit on every request so it should be able to be as light as possible.

Load the model only if you'll interact with it. This will keep the memory usage low.

Don't use the model directly from the view. All the data the view needs should come from the controller.