How to create Joomla Component?

Let’s call this component xxx, so the folder is given this name prefixed by “com_”.

Basic files required build a component
Frontend files:
xxx.php
xxx.html.php (optional)

Administration files:
admin.xxx.php
admin.xxx.html.php (optional)
toolbar.xxx.php
toolbar.xxx.html.php (optional)

Class files
xxx.class.php (optional)

Install files
xxx.xml
install.xxx.php (optional)
uninstall.xxx.php (optional)

Frontend files

Files in this group are used to display a component to website visitors. Meaning if you link a component to a menu item (using Menu -> Main Menu -> New -> Component), these files control what people see when they click on that menu.

All front end files must have the following line to prevent being loaded directly in the browser:


<?php
/** ensure this file is being included by a parent file */
defined('_VALID_MOS') or die('Direct Access to this location is not allowed.');
?>

xxx.php
This is the default file loaded by Joomla to display a component. You can have something as simple as displaying data from text file or database, or a switch statement that calls different functions according to what user choose.

A simple switch statement can look like this:


<?php
switch($task) {
case 'show_form':
// Display a form
break;
case 'save':
// Save data from the submitted form
break;
default:
// Perhaps display some data, etc
break;
}
?>

Where does $task variable come from? You need to define it in your form (as hidden variable) or include it in links so it can be sent to the server. You can use any variable name but ‘task’ is kind of the standard used in most of the components. Other variables that you need to pass to the server are $option (directory name of your component, e.g. com_mycomp) and $Itemid (menu ID of your component). Variable $Itemid is used to generate pathway and highlight the active menu. If you remove it from the URL, the page is still loading correctly but none of the menu is highlighted and pathway is not displayed correctly.

In this page you can also use $mainframe and $database objects to get configuration values, get information of the logged in user, run queries, etc.

xxx.html.php (optional)
This is where you should put GUI classes and functions. Of course you can dump everything in xxx.php file above but it is better to have them here.

Joomla’s common convention used for class name is HTML_xxx. There is no need to create a constructor function because we won’t create an instance for this class.


<?php
class HTML_xxx {
function displayForm() {
// Print form here
}
function displayData($db_result) {
// Print query result in a nice table, etc
}
}
?>

All forms must be submitted to index.php. You can use sefRelToAbs() function to convert a URL into search engine friendly form:


<?php
echo '';
?>

So how do we link these files? $mainframe object provides function called getPath() to do this easily. Just add the following line to xxx.php


<?php
// Load the HTML class
require_once($mainframe->getPath('front_html'));
?>

The string ‘front_html’ is Joomla predefined key to load xxx.html.php file.

Now we can call the above functions from xxx.php as follows:


<?php
HTML_xxx::displayForm();
HTML_xxx::displayResult();
?>

Administration files

Files in this group are used to display your component to Joomla administrator. This means you will access these files through Administration page.

All files must have the following lines to prevent being loaded directly in the browser and to kick out unauthorised users:


<?php
// ensure this file is being included by a parent file
defined('_VALID_MOS') or die('Direct Access to this location is not allowed.');
// ensure user has access to this function
if (!($acl->acl_check('administration', 'edit', 'users', $my->usertype, 'components', 'all') || $acl->acl_check('administration', 'edit', 'users', $my->usertype, 'components', 'com_dailymessage'))) {
mosRedirect('index2.php', _NOT_AUTH);
}
?>

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <div> <span> <table> <td> <tr> <b> <i> <u>
  • Lines and paragraphs break automatically.
  • You may post PHP code. You should include <?php ?> tags.
  • Use the special tag [adsense:format:group:channel] or [adsense:flexiblock:location] to display Google AdSense ads.

More information about formatting options

To combat spam, please enter the code in the image.