The Joomla MVC classes
Written by Administrator   
Wednesday, 03 March 2010
Article Index
The Joomla MVC classes
JController
JModel
JView
The MVC calling sequence

 

MVC wiring - the calling sequence

Now that you know the details of the three MVC classes, the typical jobs that they do, and how they relate to one another it is time to see how to connect them up in a system. Essentially this is what happens as part the standard operation of an MVC-based component or plug-in and you can read about it in practice in Building a Joomla component using Eclipse for PHP. However, it is worth creating a summary including the names used for the default objects - after all most components start out using default objects at least.

Banner

Assume that the component is called mycomponent and is stored in com_mycomponent.

First off everything begins with the components initial page which isn't really part of the MVC structure. This is called mycomponent.php and it is stored in the com_mycomponent directory.

This should create the default controller (or a specific controller if one is specified as part of the URL). The default controller's name is controller.php in the same directory as the initial page.

The controller's constructor should register the tasks that it can handle ready to be called. Finally the initial page should use the controller's execute method to dispatch the task specified in the URL. Clearly if the constructor hasn't set up the tasks correctly then this isn't going to work. The task is often just the default display task.

According to the task that the controller has been asked to perform it can create the model and or the view and then causes the view to display something by calling the controller's inherited display method.

The parent display method initialised and creates the default view - usually called mycomponentViewmycomponent stored in the Views\mycomponent subdirectory. It then creates an instance of the model which is by default called mycomponent.php and is stored in the models directory. Finally it calls the view's display method.

What happens next varies according to what data the view needs from the model but usually the view's display method calls the model's method to obtain data which it then displays using its inherited display method which calls the default template usually called default.php and stored in the views/mycomponent/tmpl directory.

This is all summarized in the figure below using the default names and typical function calls.

callsequence

 

It is important to realise that you can vary this call sequence to suit your own needs and it doesn't often correspond to exactly what you will find if you examine the code of a real component. Some component writers have taken the MVC pattern to heart, others use it only in part and sometimes in a muddled way.

If you would like to see another example then look out for the forthcoming article on building an Administrative or back end data entry component using the MVC pattern.

To stay informed when new Joomla! articles appear why not sign up to the site or follow us on Twitter.

Banner

<ASIN:0596804946>

<ASIN:1847195164>

<ASIN:0321590589>

<ASIN:143021628X>



Last Updated ( Wednesday, 03 March 2010 )