Page 1 of 5You might know what the MVC pattern is all about, but to make the best of it in Joomla you need to understand the three MVC classes it provides.
Joomla is an object-oriented system that uses the MVC – Model, View Controller – design pattern wherever it possibly can.
For example, you can use the MVC approach when you are building a new module, component or plug in and its also used in most of the important inner workings of Joomla. However while the MVC idea is simple enough the proliferation of classes and objects in use can make it seem complicated so let's see if it can be made clear.
The first thing to say is that Joomla's approach is MVC plus a little.
In traditional MVC designs there are just three objects:
- The Controller – designed to co-ordinate everything that happens
- The Model – designed to manipulate the data
- The View – designed to interact with the user
This is the rough fundamental idea that then goes on to be interpreted in lots of different ways. Don't expect to encounter a single authorativite way to define and build MVC applications – it's all a matter of judgment.
The basic design is intended to separate the logic of what should happen, how it happens and the presentation. The View interacts with the user but passes that interaction on to the Controller to determine what should happen and to make it happen.
Together the View and the Controller constitute the user interface of the application. The Model simply responds to the Controller or the view by either storing or retrieving some data. The MVC pattern also allows the View and the Model to talk directly to each other.
The aim in the MVC approach is to try and decouple the concerns of each of the components as much as possible. In principle it should be possible to substitute a different View or Model and everything should still work as before.
In Joomla things are set up so that there can be multiple possible views and controllers each used according to the URL specified.
In addition there are also two additional components in the design. The first is an entry component which creates the controller and sets everything into action. There is also a template component which provides a division of the traditional View component into data format and presenation.
In Joomla the View object is responsible for manipulating the data provided by the Model into a format that can be displayed by the template object. For example, raw data coming from a database is processed by the View into a form that is suitable for inclusion into an HTML or PDF document, say, and then this is finally laid out using a template which determines what fonts are used, colours and exact layout.
In simple cases there is very little for the View to actually do, making it look as if the template object is doing all of the work of the View.
The Joomla MVC classes
The Joomla framework provides base classes for each of the three MVC components: JController, JModel and JView. When you create a new Joomla MVC application it saves a great deal of time and effort to base your MVC classes on these.
Let's take a look at each one in turn.