There are many Joomla conventions that are important to understand, you can go outside them if you want but you may make life harder to yourself and your users if you do. One of these conventions is that Joomla components typically use a design pattern called MVC, short for Model-View-Controller. Understanding this is something that even experienced Joomla programmers sometimes struggle with. The goal of this article is to provide some explanation.
The underlying concept of the MVC pattern is that it splits the application development into distinct areas:-
- controller - controls the main programming logic, decides which tasks are to be performed, loads the approprite model and loads the appropriate view. For a more complex application there will also be sub-controllers that control specific areas of the application.
- model - does the actual data processing. The model should be independent of the controller and view, it should not call methods in the controller or view. Its function is not to determine the programming logic but to handle data
- view - called by the controller, receives the data from the model, handles the output through the extension template
There is room for quite a bit of personal choice in programming style in this, about how you organize your code. In fact you can choose not to use the MVC pattern at all, however there many good reasons to do so:-
- Having a system of organization makes your code easier to understand. A well-written extension should be clear and logical, even a non-programmer should be able to read the code and gain a rough idea of what it is doing. If you write open source code the expectation should be that other developers may want to modify it in future. Following the MVC design pattern makes it a lot easier for others to find the code that requires modification. Also you will make life much easier for yourself a few months down the line when you have forgotten the details of your own code (if you write a lot of code this will happen sometimes).
- Having a logical system of organization encourages clear thinking about what your extension should be doing at each point, and makes it more likely that you will write bug free code. One of the harder things in programming is often knowing where to start, the solution is to divide and conquer, by which I mean focus on one task at a time, the MVC pattern is very helpful with this.
- To aid programming using the MVC pattern Joomla contains a series of classes: JController, JModel and JView and their child classes. If you follow the pattern then you can make use of these classes to cut down on basic programming tasks. These classes also include some security features, using them properly should enable you to write more secure code.
- Using the view/layout pattern which is part of MVC allows the use of template overriding with your extension, making it easier for site designers to modify the final output without messing up your core code
The main reason for not using the MVC pattern is if you want to make an extension that is completely independent of Joomla. When Joomla calls an extension it hands over the entire responsibility for the results to the extension, so actually the extension can do anything, and be organized in any way you want. It can be a completely independent application. You might want to do things this way if you are making an extension that is not GPL licensed, and want to avoid the licensing problems that result from using Joomla code in your extension (which would require that your extension be GPL licensed).
However this is not recommended and for the purposes of this discussion we will assume that you prefer to make a proper Joomla extension and use the MVC pattern.
MVC In Practice
Joomla URLS Decoded
If you turn off SEF URLS in the configuration of a Joomla site you will see that a raw Joomla URL looks something like this:-
http://example.com/index.php?option=com_content&view=article&id=35:professionals&catid=19&Itemid=260
This URL tells you a lot about what Joomla is actually doing here. The index.php file loads the Joomla application, which then hands over control to the component named in the option URL parameter, in this case the core content component. The component loads the required controller which then executes the required task. Since there is no controller specified in the URL parameters in this case the default controller for the component will handle things. Similarly there is no specific task specified, so the application will call the controller's default task, which is normally to load the view and display the output. In this case the 'article' view is required - the id and catid parameters allow the component to determine which article will be displayed, these parameters are used by the model to get the correct data. The remaining Itemid parameter is used by Joomla to keep track of which menu item the eventual page corresponds to: this is used to determine which modules should be loaded on the page.
Sometimes of course you want to be able to do more than the default, your extension can do this by including a task parameter:-
http://example.com/index.php?option=com_example&task=item.save&id=42&Itemid=103
This example URL might be used for submitting a form which has been used to edit an item. The task parameter tells Joomla that the 'item' controller will handle things, and this controller will execute its 'save' method. You don't normally need to include a 'view' parameter in an URL such as this, normally once the 'save' task has been executed, the component will then redirect the user to a default view, probably of the item that has just been saved:-
http://example.com/index.php?option=com_example&view=item&id=42&Itemid=103
File and Folder Structure
You can see the MVC pattern reflected in the folder structure of most Joomla components. For example if you look at the content component you will see that it contains folders called 'controllers', 'models' and 'views'. The main controller will be in the root folder of the extension, by convention it will include only one method, the default display() method. Other tasks will be handled by sub-controllers, which can be found in the 'controllers' folder.
The pattern is also apparent in a simplified form in the structure of Joomla modules. There is normally a main module file, which can be considered a simple controller; a helper file which actually does the work of the module (the model), and one or more template files in a folder called 'tmpl'. To see an example of this, take a look at the Joomla articles_news module.
Joomla plugins are generally far too simple to require the MVC pattern. They normally consist of a single file. However if you want to write a complex plugin it would seem like a good idea to follow the MVC pattern as far as practical. In particular it is not difficult to create a template for your plugin output, even though Joomla does not officially support this.