In this article I will present 10 essential features to use when developing a website with Joomla CMS, they can be a good source for a Joomla novice developer, which will enable him to know best practices to use in such project.
Take into consideration that these tips are tested in version 1.5 of Joomla, and can more or less do not work into the next version 1.6.
1 - JRequest
In most applications in PHP, you often use the $ _POST, $ _GET and $ _REQUEST, thing that makes it easy for a web developer when it comes to forms.
However, there are security risks to consider. Anyone can easily inject code into these variables if they are not adequately filtered. Thats why we need to use the JRequest class.
This class is used to do many things, including the filtering of data. In Joomla application, we must always go through JRequest to retrieve data from a query ($_POST, $ _GET ,...).
1 2
|
$count = JRequest::getInt('count', 0); $userValue = JRequest::getString('uservalue', null);
|
There are several types of filtering, simply consult the file "
libraries/joomla/environment/request.php" for a list of methods that use little.
2 - addScript and addStylesheet
Often when people new to Joomla and need to use a snippet of javascript or CSS, they use the tags <style>, <script> and <link> to add their codes, however, it is not advisable to add these files this way.
To remedy this problem, Joomla provides methods that simplify the addition of such scripts, and this in a proper way.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
// To add javascripts / ** * @ Param Filename * @ Param Path to filename * @ Param Use Mootools 1.11 library */ JHTML::script('myscript.js', 'media/js/', false); Or // Get document instance $document = & JFactory::getDocument(); // Add the script to our document $document->addscript('media/js/myscript.js'); Or // Add inline script $script = 'alert ("Hello world !");'; // Add the script to our document $document->addScriptDeclaration($script), // To add scripts CSS / ** * @ Param Filename * @ Param Path to filename * @ Param array params to be added to the <link> tag */ JHTML::stylesheet('mystyle.css', 'media/css/', array()); Or // Get document instance $document = & JFactory::getDocument(); // Add the script to our document $document->addStyleSheet('media/css/mystyle.css'); Or // Add inline style $style = 'body {color: # 333;}'; // Add the script to our document $document-> addStyleDeclaration($style);
|
3 - JFile and JFolder
To use files and folders, Joomla provides two classes wich include various operations that you can use in your development.
You can see the complete list in these files : "libraries/joomla/filesystem/file.php" and "libraries/joomla/filesystem/folder.php"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
// Import file and folder class's jimport('joomla.filesystem.file'); jimport('joomla.filesystem.folder'); // Verify if file exists $fileExists = JFile::exists('tmp/temporary_file.php'); // Return true or false // Delete a folder $folderDeleted = JFolder::delete('tmp/temporary_folder/'); // Return true or false // Get filename $filename = JFile::getName('tmp/temporary_folder/file.ext'); // Return : file.ext // Create a folder with CHMOD : 0655 $folderCreated = JFolder::create('tmp/folder1/', 0655); // Return true or false
|
4 - Session Variables
Joomla simplifies the use of session variables in a class dedicated to this task.
1 2 3 4 5 6 7 8
|
// Get session $session = & JFactory::getSession(); // Get a variable $userfield1 = $session->get('userfield1'); // Set a variable $userfield1 = $session->set('userfield1', 'your_value_here');
|
5 - Quote and nameQuote
It is always advisable to use single quotes for values in a SQL query. In Joomla even experienced developers forget to use the method "Quote" or "nameQuote, these two methods allow us to delimit a string in a SQL statement regardless of what type of database you use.
1 2 3 4 5 6 7 8
|
// Exemple of use $db = & JFactory::getDBO(); $query = 'SELECT ' . $db->nameQuote('username') .' FROM #__users' .' WHERE ' . $db->nameQuote('id') . ' = ' . $db->Quote('1'); // Result : SELECT `username` FROM #__users WHERE `id` = '1'
|
6 - Path constants
There are a lot of constants used everywhere, and provide the URLs to specific locations in the site. These are essential ones :
1 2 3 4 5 6 7 8 9 10 11 12 13
|
// Basic folders Urls JPATH_SITE // Url to root JPATH_ADMINISTRATOR // Url to admin folder JPATH_XMLRPC // Url to XMLRPC folder JPATH_LIBRARIES // Url to libraries folder JPATH_PLUGINS // Url to plugins folder JPATH_THEMES // Url to themes root folder JPATH_CACHE // Url to cache folder // Special Urls JPATH_COMPONENT // Url to a FE or BE component JPATH_COMPONENT_SITE // Url to a FE component JPATH_COMPONENT_ADMINISTRATOR // Url to a BE component
|
7 - JRoute
We should never put internal links without having passed through JRoute, it will cause us problems if we force the routing option : url rewritting
1 2 3 4 5
|
// Bad echo 'index.php?option=com_users&task=logout'; // Good echo JRoute::_('index.php?option=com_users&task=logout');
|
8 - JText
This facilitates the task when you want to make a multilingual website. Each component, module or plugin even has its own language file, which allows us to translate the interface of the site easily.
The language files are usually in the folder "language" for the FrontEnd, and "administrator/language" for the BackEnd. Inside, there are various folders concerning the languages installed on the site.
There is a naming convention for the version of Joomla 1.5 which says that each language file should start with short name of the language, followed by item name (the component, module or plugin)
1 2 3 4 5 6 7 8 9 10 11
|
// In en-GB folder : en_GB.com_example.ini EXAMPLE_TEST_LINE=Just an example // In fr-FR folder : fr_FR.com_example.ini EXAMPLE_TEST_LINE=Juste un exemple // Somewhere in a view of our example component echo JText::_('EXAMPLE_TEST_LINE'); // This will show : Just an example, if our site is in english // This will show : Juste un exemple, if our site is in french
|
Always consider using
JText, we never know when we can make our site multilingual. If
JText does not find a value assigned to
EXAMPLE_TEST_LINE, it simply show:
EXAMPLE_TEST_LINE
9 - defined ('_JEXEC')
This is very useful to stop direct access to a PHP file. We find this line anywhere in the Joomla PHP files.
1 2
|
// no direct access defined('_JEXEC') or die('Restricted access');
|
This line must be placed in the beginning of each PHP file.
10 - JError
This class will greatly help us to display the error messages of our component.
1 2 3 4 5
|
// Some examples JError::raiseError(403, JText::_("ALERT NOT AUTH")); JError::raiseError(404, JText::_("Category not found")); JError::raiseWarning( 403, JText::_('ALERT NOT AUTH') ); JError::raiseNotice( 500, JText::_('EMAIL_NOT_SENT' ));
|
Conclusion
Finally, there are many other tricks, but a single article is not enough. I hope it will bring you new ideas about Joomla. Otherwise I'm here for your feedback