You are on page 1of 4

COMPOENT Part 1

<?php
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

$task = JRequest::getVar( 'task' );
$name = JRequest::getVar( 'name', 'John' );

switch ($task) {
case 'show':
default:
echo 'Hello ' . $name . ' in our World!';
break;
}

?>

You may have asked yourself ‘Whats the JRequest::getVar() for? And whats it
doing with that variable?’. To make Joomla more secure, all global variables
should be read through this function. It removes the possibility for code
injection and/or SQL injection. You can also define a default value (as you can
see in line 6).

In Joomla! the variable ‘task’ is commonly used to switch between different, you
might guess it, tasks. Besides that, there is the variable ‘option’, whose value is
used to determine which component is supposed to be loaded. So never use it for
anything else, otherwise you might get funny errors.
<?php
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

// require the html view class
jimport( 'joomla.application.helper' );
require_once( JApplicationHelper::getPath(
'front_html', 'com_hello' ) );

$task = JRequest::getVar( 'task' );
$name = JRequest::getVar( 'name', 'John' );

switch ($task) {
case 'show':
default:
hello_HTML::show( $name );
break;
}
?>

<?php
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

class hello_HTML {

function show( $name ) {
echo 'Hello ' . $name . ' in our World!';
}

}
?>

With the JApplicationHelper::getPath() function we determine the path to the
file with the HTML ouput. In the switch statement we use the call for the
function ‘show’ in the class ‘hello_HTML’ instead of the echo, so now we can
create whatever HTML-output in the hello.html.php file instead of messing
around in the real logic behind our component. Its also very easy to update the
component. In case we made an error in our logic, we just have to replace the
logic file and don’t mess with possible modifications the user has made in the
.html.php-file.
COMPOENT Part 2

/**
* Hello Model for Hello World Component
*
* @package Joomla.Tutorials
* @subpackage Components
* @link http://dev.joomla.org/component/option,com_jd-
wiki/Itemid,31/id,tutorials:modules/
* @license GNU/GPL
*/

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

jimport( 'joomla.application.component.model' );

/**
* Hello Model
*
* @package Joomla.Tutorials
* @subpackage Components
*/
class HelloModelHello extends JModel
{
/**
* Gets the greeting
* @return string The greeting to be displayed to the user
*/
function getGreeting()
{
return 'Hello, World!';
}
}
You will notice a line that starts with jimport. The jimport function is used to
load files from the Joomla! framework that are required for our component. This
particular statement will load the file
/libraries/joomla/application/component/model.php. The ‘.’s are used as
directory separators and the last part is the name of the file to load. All files are
loaded relative to the libraries directory. This particular file contains the class
definition for the JModel class, which is necessary because our model extends
this class.

- models without ?> in the end