How to Create Joomla Component from Scratch

Advertisement

Will this article is exciting isn’t it? in this article I will explain how to create Joomla component from scratch, you will need to know PHP and mySQL for database first.

In this article 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 sample. Therefore, you will have to create two folders named com_sample.

  • 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_sample folder, create the sample.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.sample.php in administrator/components/com_sample. 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_sample 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 sample.php and add the following code, then refresh the browser:


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

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_sample/sample.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_sample/admin.sample.php.


<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
echo 'Sample Component';
?>

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.

Registering Your Component in the Database

You now know how to access both the front end and back end of the component. Although you could keep typing in the URLs each time you wanted to execute a piece of code, this will not be acceptable to your users. Navigation can be provided if you register the component in the database by adding a row to the components table.

We will perform this registration using the following query. It is assumed that your database prefix is jos_. If not, replace jos_ with the prefix you chose. If you prefer to work with direct SQL statements on a command-line interface, enter the following query in your console.


INSERT INTO jos_components (name, link, admin_menu_link, admin_menu_alt, 'option', admin_menu_img, params)
VALUES
('Sample Component', 'option=com_sample', 'option=com_sample', 'Sample Component', 'com_sample', 'js/ThemeOffice/component.png', '');

Adding this record gives the system some basic information about your component. It states the name you want to use for the component, which can contain spaces and punctuation. You can put in specific links to go to both the front end and back end. The image to be used on the Components menu can be specified. Also as the description in the browser status bar can be made available. It is not necessary to addthis query while developing the component; once you create the basic directories and files, your component is ready to be executed. However, it does add a menu item in the back-end and makes it possible to add an appropriate link in the front-end without hard coding a URL.

After the record is successfully entered, go to any page in the back end and refresh it. When you move the mouse cursor over the Components menu you should see the new option:

Now that the component is registered, you can also create a link for the front end. Go to Menus | Main Menu and click New. From this screen, select Component Sample. Enter Sample Component or any name best for you.

Now click Save and go to the front end. You should now see Sample component listed as an option.

You could just break out your PHP skills and start coding the component, ensuring all front-end requests go through http://localhost/joomla/index.php?option=com_sample and all back-end requests go though http://localhost/ joomla/administrator/index.php?option=com_sample. Joomla! is flexible enough to let you do as you please. In some cases, you will have existing code that you may want to use and you will need to split it into appropriate files. But for the component sample, you will start a new Joomla! component from scratch. You have the opportunity to design everything with Joomla’s toolbars, users, database classes, and libraries in mind. These elements will save you a lotof time once you understand how theywork.

Creating Toolbars

Throughout the back end, all the core components implement toolbars with similarbuttons for saving, deleting, editing, and publishing items. You can use these buttons in your component so that frequent administrators will have a seamless experience.

To start, create the toolbar.sample.html.php file in the administrator/components/com_sample folder and enter in the following code:


<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
class TOOLBAR_sample {
function _NEW() {
JToolBarHelper::save();
JToolBarHelper::apply();
JToolBarHelper::cancel();
}
function _DEFAULT() {
JToolBarHelper::title( JText::_( 'Sample Component' ),
'generic.png' );
JToolBarHelper::publishList();
JToolBarHelper::unpublishList();
JToolBarHelper::editList();
JToolBarHelper::deleteList();
JToolBarHelper::addNew();
}
}
?>

The line containing require_once(…) uses the getPath() member function of the JApplicationHelper class. The call to getPath() allows you to call up the toolbar.sample.html.php file without committing to a component name. Later, even if you change the name to ‘Component’ and also change the filenames, this line of code will still load the output code for the toolbar with no modification.

You may be wondering why we are creating two files to begin with, toolbar.sample.php and toolbar.sample.html.php. The preferred coding style among component developers is to keep the processing logic in a file completely separate from where the actual output takes place. This makes it easier to add features later and to potentially share the code with others.

After toolbar.sample.php loads the file with the output class, you need to decide which toolbar should be displayed. The request variable $task is automatically registered in global scope by Joomla! and is used to direct the logic flow within the component. With your toolbar code in place, refresh the browser in the back end and go to Sample component under Components and you can see that the Joomla toolbars are added.

There are lot of Joomla toolbar available, you can search it in joomla site.

Summary

The basic files necessary to build the component are now in place. The rest of the Joomla! installation now knows that this component is available for front end and back end use. By using standard HTML and CSS classes, the component has a look and feel similar to the other components in the system, making it easy to use with different templates. Basic toolbars are available to the component and can be assigned to different screens by using the $task variable.

Hope that helps, happy coding 😉

Advertisement