Building a Joomla component using Eclipse for PHP
Building a Joomla component using Eclipse for PHP
Written by Ian Elliot   
Monday, 28 September 2009
Article Index
Building a Joomla component using Eclipse for PHP
Creating the XML file
Building the files
The view and template

Building the files

The next stage is to provide each of the files with some default content. This also provides an opportunity to say a little about how they fit together.


The overall approach is to use the Model View Controller architecture - the Controller uses the Model to supply any data and the View to display the data. As this is an HTML application the pure MVC approach is a little more complicated as there is also a loader which gets things going by creating the controller and a template which is used by the view to specify the layout of the final HTML page.

For more information on the MVC approach see

Our custom model, view and controller classes are descended from JModel, JView and JController respectively.

It is also useful to know at this stage that our component will be loaded by a URL something like like:

index.php?option=com_ mycomponent 
    &view= mycomponent

and Joomla works out from this how to load and run the component.

The controller is a good place to start because, even though it isn't the first thing to be loaded it does a lot of the work:


In this file you define the controller class. As our sample component doesn’t actually do much the code is very simple and just called the parent's display method:

defined( '_JEXEC' ) or
die( 'Restricted access' );
class MyComponentController
extends JController
function display()

Notice that while the file is called controller.php the controller class is called MyComponentController. Important note: in general use lower case file and folder names but you can use capitalised class names.


This is the file Joomla loads to actually run your component so it contains the startup code and it creates the controller object which you have already defined in controller.php.

Assuming the URL used to load your component is:

index.php?option=com_ mycomponent 
&view= mycomponent

then it will load mycomponent.php stored in the com_mycontroller directory and this is where your component will be installed automatically. In this case the task for the controller to carry out it the default "display" task using the mycomponent view.

The loader code in mycomponent.php is:

// No direct access
defined( '_JEXEC' ) or
 die( 'Restricted access' );
// Require the base controller
require_once( JPATH_COMPONENT.DS.
'controller.php' );
// Require specific controller if requested
if($controller =
JRequest::getWord('controller')) {
if (file_exists($path)) {
require_once $path;
} else {
$controller = '';
// Create the controller
$classname = 'MyComponentController'
$controller = new $classname( );
// Perform the Request task
JRequest::getVar( 'task' ) );

// Redirect if set by the controller

If you read this through you should be able to work out that this simply creates an instance of the controller class we defined earlier in controller.php. The only complication is that the request that loads the component might specify a different controller. The getVar("controller") returns any controller that is specified within the URL and it is this that performs the controller function in place of your default controller. That is it is instantiated not the basic controller we have supplied. For the moment we can assume that it is our basic controller that is used.

So the line:

$controller   = new $classname( );

creates an instance of the controller class we defined earlier in the controller.php file. i.e.:

class MyComponentController 
extends JController;

The line:

$controller->execute( JRequest::getVar( 'task' ) );

retrieves the task specified in the URL. In this case the task isn't specified and so the default display task is assumed. This results in the display method of the instance of MyComponentController being called.




Last Updated ( Wednesday, 03 March 2010 )

RSS feed of all content
I Programmer - full contents
Copyright © 2018 All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.