Version 16, last updated by pagchen at 23 Mar 22:16 UTC
Model-View-Controller
This wiki has been updated for version 0.7-alpha4
1. Overview
It is common to split an application into separate layers that run on different computers: the presentation/user interface (UI), business logic, and data access. In MVC the presentation layer is further separated into Model, view and controller, resulting in three layers.
MVC is often seen in web applications, where the view is the actual HTML or XHTML page, and the controller is the code that gathers dynamic data and generates the content within the HTML or XHTML. Finally, the model is represented by the actual content, which is often stored in a database or in XML nodes, and the business rules that transform that content based on user actions.

Though MVC comes in different flavors, control flow is generally as follows:
- The user interacts with the user interface in some way (for example, presses a mouse button).
- The controller handles the input event from the user interface, often via a registered handler or callback.
- The controller notifies the model of the user action, possibly resulting in a change in the model's state. (for example, the controller updates the user's shopping cart)
- A view uses the model indirectly to generate an appropriate user interface (for example, the view lists the shopping cart's contents). The view gets its own data from the model. The model and controller have no direct knowledge of the view.
- The user interface waits for further user interactions, which restarts the cycle.
By decoupling models and views, MVC helps to reduce the complexity in architectural design and to increase flexibility and reuse of code.

2. Model
Models would generally have a plural name, they describe a collection and allow you to retrieve either a list or an item:
$model = KService::get('com://admin/mycomp.model.categories');
$model->getList();
$model->getItem();
Table names are plural as well. Convention in Nooku is to use :
#__mycomp_peoples (where #__ in a Joomla world default translates to jos_ )
primary key: mycomp_person_id
$id = $row->id works as Nooku converts automatically between 'mycomp_person_id' and 'id'
2.1 States and the fluent interface
The model's fluent interface can be used to change states on the fly and fetch data with getList() and getItem().
For instance:
KService::get('com://admin/harbour.model.boats')
->order('name')->direction('desc')->limit(10)->offset(20)->enabled(1)
->getList();
Both order, direction, limit, offset and enabled are model states in this case.
Some default states are inserted in KModelTable->__construct() which you can use right away. You can also set your own states in your own models if you want to. Example:
In your model's __construct method you introduce a new state:
$this->_state->insert('captain', 'string');
And then you instruct the model to respond to the state being used, inside the _buildQueryWhere method:
if($this->_state->captain) {
$query->where('captain', '=', $this->_state->captain);
}
3. Controller
Todo
4. View
Todo
5. References
- http://www.phpwact.org/pattern/model_view_controller
- http://en.wikipedia.org/wiki/Web_application_framework
- http://java.sun.com/blueprints/patterns/MVC-detailed.html
- http://www.theserverside.com/patterns/thread.tss?thread_id=22143
- http://st-www.cs.illinois.edu/users/smarch/st-docs/mvc.html
Also see : Books