Developing the Joomla! Component and Understanding its Structure

by Joseph L. LeBlanc | November 2007 | Joomla! MySQL Content Management Open Source PHP

In this article by Joseph L. LeBlanc, you will learn about the architecture, design, and requirements of a general Joomla! component. You will also see how the component gets executed and is registered with the database. At the end, you will learn to create toolbars.

Joomla!'s Component Structure

Joomla! employs a specific naming scheme, which is used by all components. Each component in the system has a unique name with no spaces. The code is split into two folders, each bearing the component name prefixed by com_. The component used here will be called reviews. Therefore, you will have to create two folders named com_reviews:

  • Create one in the folder named components for the front end.
  • Create one in the folder named components within the administrator folder for the back end.

When the component is loaded from the front end, Joomla! will look for a file with the component's unique name ending in a .php extension. Within the components/com_reviews folder, create the reviews.php file. Similarly, running it in the back end assumes the presence of a file prefaced with admin. followed by the component name and ending in .php. Add the file admin.reviews.php in administrator/components/com_reviews. Leave both the files empty for the moment.

Executing the Component

All front-end requests in Joomla! go through index.php in the root directory. Different components can be loaded by setting the option variable in the URL GET string. If you install Joomla! on a local web server in a directory titled joomla, the URL for accessing the site will be http://localhost/joomla/index.php or something similar. Assuming this is the case, you can load the component's front end by opening http://localhost/joomla/index.php?option=com_reviews in your browser. At this point, the screen should be essentially blank, apart from the common template elements and modules. To make this component slightly more useful, open reviews.php and add the following code, then refresh the browser:

<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
echo '<div class="componentheading">Restaurant Reviews</div>';
?>

Your screen will look similar to the following:

Developing the Joomla! Component and Understanding its Structure

You may be wondering why we called defined() at the beginning of the file. This is a check to ensure that the code is called through Joomla! instead of being accessed directly at components/com_reviews/reviews.php. Joomla! automaticallyconfigures the environment with some security safeguards that can be defeated ifsomeone is able to directly execute the code for your component.

For the back end, drop this code into

administrator/components/com_reviews/admin.reviews.php:

<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
echo 'Restaurant Reviews';
?>

Go to http://localhost/joomla/administrator/index.php?option=com_reviews and compare your result to this:

Developing the Joomla! Component and Understanding its Structure

Joomla!'s Division between Front End and Back End

For all Joomla! components, code empowering the back-end portion is kept away from the front-end code. In some instances, such as the database table class, the back end will use certain files from the front end, but otherwise the two are separate. Security is enhanced as you are less likely to slip the administrative functions into the front-end code. This is an important distinction as the front end and back end are similar in structure.     

The following folder diagram shows the Joomla! root with the administrator folder expanded:

Developing the Joomla! Component and Understanding its Structure

Notice that the administrator folder has a structure similar to the root folder. It is important to differentiate between the two, else you may place your code in the wrong folder and it will fail to execute until youmove it.

Sign up for a Packt account to see the rest of this article

Now that you've read a few articles, you might want to consider signing up for a Packt account. It takes a matter of seconds, will give you access to all the articles on PacktPub.com, and once you've signed up you'll be returned here to carry on reading your article.

Furthermore, you'll gain access to nine free ebooks, and be offered a free trial of PacktLib, Packt's online library. Simply enter your details here, or log in to your existing account.

Log in

...or register

Fantastic by
At last!!! A tutorial, that doesn't make Joomla component development, seem like rocket science. You are gud dude! Great work. Keep it up!!
Try article by
I really liked the article, and the very cool blog
thank by
thanks so much..this is such a great help!! thanks thanks!!
thanx by
this is my first time doing such awesome thing, thanx alot
good one by
awesome tutorial.. It changed my mind about joomla components !! Really great information for someone who just started to create own joomla components. Thanks a lot ,,
very good article by
thanks for providing useful information regarding component
Great tutorial by
Great tutorial to get started and to get hold of things. Thanks a ton.me alot,i was looking for this for a while! it Helped me alot Thank you so much. This tutorial is a great start for understanding Joomla components,very good point to point info.This is very much helpfull,very nice and easy to understand, good work Good for beginner !! THANK YOU.....................
I like it verymuch by
Really awesome... I am newbee to joomla.. its very useful to me.. thanks alot.
Great article by
Great tutorial to get started and to get hold of things. Thanks a ton.
Superb by
THanks, was looking for this for a while!

Post new comment

Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Sort A-Z