J3.x

Difference between revisions of "Developing an MVC Component/Basic backend"

From Joomla! Documentation

< J3.x:Developing an MVC Component
(→‎Create the view: working code from the sources archive)
m (Removed the review tag - I've followed the tutorial this far and it's good.)
(20 intermediate revisions by 7 users not shown)
Line 1: Line 1:
 
{{:J3.1:Developing a MVC Component}}
 
{{:J3.1:Developing a MVC Component}}
 
{{review}}
 
  
 
This is a multiple-article series of tutorials on how to develop a Model-View-Contoller [[Component]] for Joomla! Version {{JVer|{{CurrentSTSVer|minor}}}}.
 
This is a multiple-article series of tutorials on how to develop a Model-View-Contoller [[Component]] for Joomla! Version {{JVer|{{CurrentSTSVer|minor}}}}.
Line 12: Line 10:
  
 
<span id="admin/helloworld.php">
 
<span id="admin/helloworld.php">
''admin/helloworld.php''
+
'''''admin/helloworld.php'''''
 
<source lang="php">
 
<source lang="php">
 
<?php
 
<?php
 +
/**
 +
* @package    Joomla.Administrator
 +
* @subpackage  com_helloworld
 +
*
 +
* @copyright  Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
 +
* @license    GNU General Public License version 2 or later; see LICENSE.txt
 +
*/
 +
 
// No direct access to this file
 
// No direct access to this file
 
defined('_JEXEC') or die('Restricted access');
 
defined('_JEXEC') or die('Restricted access');
+
 
// import joomla controller library
 
jimport('joomla.application.component.controller');
 
 
 
// Get an instance of the controller prefixed by HelloWorld
 
// Get an instance of the controller prefixed by HelloWorld
 
$controller = JControllerLegacy::getInstance('HelloWorld');
 
$controller = JControllerLegacy::getInstance('HelloWorld');
  
// Get the task
 
$jinput = JFactory::getApplication()->input;
 
$task = $jinput->get('task', "", 'STR' );
 
 
 
// Perform the Request task
 
// Perform the Request task
$controller->execute($task);
+
$input = JFactory::getApplication()->input;
+
$controller->execute($input->getCmd('task'));
 +
 
 
// Redirect if set by the controller
 
// Redirect if set by the controller
 
$controller->redirect();
 
$controller->redirect();
Line 44: Line 44:
 
<source lang="php">
 
<source lang="php">
 
<?php
 
<?php
 +
/**
 +
* @package    Joomla.Administrator
 +
* @subpackage  com_helloworld
 +
*
 +
* @copyright  Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
 +
* @license    GNU General Public License version 2 or later; see LICENSE.txt
 +
*/
 +
 
// No direct access to this file
 
// No direct access to this file
 
defined('_JEXEC') or die('Restricted access');
 
defined('_JEXEC') or die('Restricted access');
+
 
// import Joomla controller library
 
jimport('joomla.application.component.controller');
 
 
 
/**
 
/**
 
  * General Controller of HelloWorld component
 
  * General Controller of HelloWorld component
 +
*
 +
* @package    Joomla.Administrator
 +
* @subpackage  com_helloworld
 +
* @since      0.0.7
 
  */
 
  */
 
class HelloWorldController extends JControllerLegacy
 
class HelloWorldController extends JControllerLegacy
 
{
 
{
 
/**
 
/**
* display task
+
* The default view for the display method.
 
*
 
*
* @return void
+
* @var string
 +
* @since 12.2
 
*/
 
*/
function display($cachable = false, $urlparams = false)
+
protected $default_view = 'helloworlds';
{
 
// set default view if not set
 
$input = JFactory::getApplication()->input;
 
$input->set('view', $input->getCmd('view', 'HelloWorlds'));
 
 
// call parent behavior
 
parent::display($cachable);
 
}
 
 
}
 
}
  
Line 84: Line 86:
 
<source lang="php">
 
<source lang="php">
 
<?php
 
<?php
 +
/**
 +
* @package    Joomla.Administrator
 +
* @subpackage  com_helloworld
 +
*
 +
* @copyright  Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
 +
* @license    GNU General Public License version 2 or later; see LICENSE.txt
 +
*/
 +
 
// No direct access to this file
 
// No direct access to this file
 
defined('_JEXEC') or die('Restricted access');
 
defined('_JEXEC') or die('Restricted access');
 
// import Joomla view library
 
jimport('joomla.application.component.view');
 
  
 
/**
 
/**
 
  * HelloWorlds View
 
  * HelloWorlds View
 +
*
 +
* @since  0.0.1
 
  */
 
  */
 
class HelloWorldViewHelloWorlds extends JViewLegacy
 
class HelloWorldViewHelloWorlds extends JViewLegacy
 
{
 
{
 
/**
 
/**
* HelloWorlds view display method
+
* Display the Hello World view
* @return void
+
*
 +
* @param  string  $tpl  The name of the template file to parse; automatically searches through the template paths.
 +
*
 +
* @return void
 
*/
 
*/
function display($tpl = null)  
+
function display($tpl = null)
 
{
 
{
 
// Get data from the model
 
// Get data from the model
$items = $this->get('Items');
+
$this->items = $this->get('Items');
$pagination = $this->get('Pagination');
+
$this->pagination = $this->get('Pagination');
  
 
// Check for errors.
 
// Check for errors.
if (count($errors = $this->get('Errors')))  
+
if (count($errors = $this->get('Errors')))
 
{
 
{
 
JError::raiseError(500, implode('<br />', $errors));
 
JError::raiseError(500, implode('<br />', $errors));
 +
 
return false;
 
return false;
 
}
 
}
// Assign data to the view
 
$this->items = $items;
 
$this->pagination = $pagination;
 
  
 
// Display the template
 
// Display the template
Line 128: Line 138:
 
<source lang="php">
 
<source lang="php">
 
<?php
 
<?php
 +
/**
 +
* @package    Joomla.Administrator
 +
* @subpackage  com_helloworld
 +
*
 +
* @copyright  Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
 +
* @license    GNU General Public License version 2 or later; see LICENSE.txt
 +
*/
 +
 
// No direct access to this file
 
// No direct access to this file
 
defined('_JEXEC') or die('Restricted Access');
 
defined('_JEXEC') or die('Restricted Access');
 +
?>
 +
<form action="index.php?option=com_helloworld&view=helloworlds" method="post" id="adminForm" name="adminForm">
 +
<table class="table table-striped table-hover">
 +
<thead>
 +
<tr>
 +
<th width="1%"><?php echo JText::_('COM_HELLOWORLD_NUM'); ?></th>
 +
<th width="2%">
 +
<?php echo JHtml::_('grid.checkall'); ?>
 +
</th>
 +
<th width="90%">
 +
<?php echo JText::_('COM_HELLOWORLD_HELLOWORLDS_NAME') ;?>
 +
</th>
 +
<th width="5%">
 +
<?php echo JText::_('COM_HELLOWORLD_PUBLISHED'); ?>
 +
</th>
 +
<th width="2%">
 +
<?php echo JText::_('COM_HELLOWORLD_ID'); ?>
 +
</th>
 +
</tr>
 +
</thead>
 +
<tfoot>
 +
<tr>
 +
<td colspan="5">
 +
<?php echo $this->pagination->getListFooter(); ?>
 +
</td>
 +
</tr>
 +
</tfoot>
 +
<tbody>
 +
<?php if (!empty($this->items)) : ?>
 +
<?php foreach ($this->items as $i => $row) : ?>
  
// load tooltip behavior
+
<tr>
JHtml::_('behavior.tooltip');
+
<td><?php echo $this->pagination->getRowOffset($i); ?></td>
?>
+
<td>
<form action="<?php echo JRoute::_('index.php?option=com_helloworld'); ?>" method="post" name="adminForm">
+
<?php echo JHtml::_('grid.id', $i, $row->id); ?>
<table class="adminlist">
+
</td>
<thead><?php echo $this->loadTemplate('head');?></thead>
+
<td>
<tfoot><?php echo $this->loadTemplate('foot');?></tfoot>
+
<?php echo $row->greeting; ?>
<tbody><?php echo $this->loadTemplate('body');?></tbody>
+
</td>
 +
<td align="center">
 +
<?php echo JHtml::_('jgrid.published', $row->published, $i, 'helloworlds.', true, 'cb'); ?>
 +
</td>
 +
<td align="center">
 +
<?php echo $row->id; ?>
 +
</td>
 +
</tr>
 +
<?php endforeach; ?>
 +
<?php endif; ?>
 +
</tbody>
 
</table>
 
</table>
 
</form>
 
</form>
</source>
 
</span>
 
 
This layout calls several sub-layout (''head'', ''foot'' and ''body''). Each sub-layout corresponds to a file prefixed by the name of the main layout (''default''), and an underscore.
 
  
Put a file ''admin/views/helloworlds/tmpl/default_head.php'' containing
 
 
<span id="admin/views/helloworlds/tmpl/default_head.php">
 
''admin/views/helloworlds/tmpl/default_head.php''
 
<source lang="php">
 
<?php
 
// No direct access to this file
 
defined('_JEXEC') or die('Restricted Access');
 
?>
 
<tr>
 
<th width="5">
 
<?php echo JText::_('COM_HELLOWORLD_HELLOWORLD_HEADING_ID'); ?>
 
</th>
 
<th width="20">
 
<?php echo JHtml::_('grid.checkall'); ?>
 
</th>
 
<th>
 
<?php echo JText::_('COM_HELLOWORLD_HELLOWORLD_HEADING_GREETING'); ?>
 
</th>
 
</tr>
 
 
</source>
 
</source>
 
</span>
 
</span>
  
 
+
''COM_HELLOWORLD_HELLOWORLDS_NAME'', ''COM_HELLOWORLD_ID'' and the others are placeholders which will later be replaced with language-specific text. The [http://api.joomla.org/cms-3/classes/JText.html JText::_] method translates a string into the current language.
''COM_HELLOWORLD_HELLOWORLD_HEADING_ID'' and ''COM_HELLOWORLD_HELLOWORLD_HEADING_GREETING'' are placeholders which will later be replaced with language-specific text. The [http://api.joomla.org/Joomla-Platform/Language/JText.html JText::_] method translates a string into the current language.
 
  
 
''checkAll'' is a javascript function defined in the Joomla core able to check all items.
 
''checkAll'' is a javascript function defined in the Joomla core able to check all items.
 
Put a file ''admin/views/helloworlds/tmpl/default_body.php'' containing
 
 
<span id="admin/views/helloworlds/tmpl/default_body.php">
 
''admin/views/helloworlds/tmpl/default_body.php''
 
<source lang="php">
 
<?php
 
// No direct access to this file
 
defined('_JEXEC') or die('Restricted Access');
 
?>
 
<?php foreach($this->items as $i => $item): ?>
 
<tr class="row<?php echo $i % 2; ?>">
 
<td>
 
<?php echo $item->id; ?>
 
</td>
 
<td>
 
<?php echo JHtml::_('grid.id', $i, $item->id); ?>
 
</td>
 
<td>
 
<?php echo $item->greeting; ?>
 
</td>
 
</tr>
 
<?php endforeach; ?>
 
</source>
 
</span>
 
  
 
''JHtml::_'' is a helper function able to display several HTML output. In this case, it will display a checkbox for the item.
 
''JHtml::_'' is a helper function able to display several HTML output. In this case, it will display a checkbox for the item.
 
Put a file ''admin/views/helloworlds/tmpl/default_foot.php'' containing
 
 
<span id="admin/views/helloworlds/tmpl/default_foot.php">
 
''admin/views/helloworlds/tmpl/default_foot.php''
 
<source lang="php">
 
<?php
 
// No direct access to this file
 
defined('_JEXEC') or die('Restricted Access');
 
?>
 
<tr>
 
<td colspan="3"><?php echo $this->pagination->getListFooter(); ?></td>
 
</tr>
 
</source>
 
</span>
 
  
 
''JPagination'' is a Joomla class able to manage and display pagination object.
 
''JPagination'' is a Joomla class able to manage and display pagination object.
Line 229: Line 222:
  
 
<span id="admin/models/helloworlds.php">
 
<span id="admin/models/helloworlds.php">
''admin/models/helloworlds.php''
+
'''''admin/models/helloworlds.php'''''
 
<source lang="php">
 
<source lang="php">
 
<?php
 
<?php
 +
/**
 +
* @package    Joomla.Administrator
 +
* @subpackage  com_helloworld
 +
*
 +
* @copyright  Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
 +
* @license    GNU General Public License version 2 or later; see LICENSE.txt
 +
*/
 
// No direct access to this file
 
// No direct access to this file
 
defined('_JEXEC') or die('Restricted access');
 
defined('_JEXEC') or die('Restricted access');
// import the Joomla modellist library
+
 
jimport('joomla.application.component.modellist');
 
 
/**
 
/**
 
  * HelloWorldList Model
 
  * HelloWorldList Model
 +
*
 +
* @since  0.0.1
 
  */
 
  */
 
class HelloWorldModelHelloWorlds extends JModelList
 
class HelloWorldModelHelloWorlds extends JModelList
Line 244: Line 245:
 
* Method to build an SQL query to load the list data.
 
* Method to build an SQL query to load the list data.
 
*
 
*
* @return string An SQL query
+
* @return     string An SQL query
 
*/
 
*/
 
protected function getListQuery()
 
protected function getListQuery()
 
{
 
{
// Create a new query object.
+
// Initialize variables.
$db = JFactory::getDBO();
+
$db   = JFactory::getDbo();
 
$query = $db->getQuery(true);
 
$query = $db->getQuery(true);
// Select some fields
+
 
$query->select('id,greeting');
+
// Create the base select statement.
// From the hello table
+
$query->select('*')
$query->from('#__helloworld');
+
                ->from($db->quoteName('#__helloworld'));
 +
 
 
return $query;
 
return $query;
 
}
 
}
 
}
 
}
 +
 
</source>
 
</source>
 
</span>
 
</span>
Line 297: Line 300:
 
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/views/helloworlds/tmpl/index.html]]''
 
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/views/helloworlds/tmpl/index.html]]''
 
* ''[[#admin/views/helloworlds/tmpl/default.php|admin/views/helloworlds/tmpl/default.php]]''
 
* ''[[#admin/views/helloworlds/tmpl/default.php|admin/views/helloworlds/tmpl/default.php]]''
* ''[[#admin/views/helloworlds/tmpl/default_head.php|admin/views/helloworlds/tmpl/default_head.php]]''
 
* ''[[#admin/views/helloworlds/tmpl/default_body.php|admin/views/helloworlds/tmpl/default_body.php]]''
 
* ''[[#admin/views/helloworlds/tmpl/default_foot.php|admin/views/helloworlds/tmpl/default_foot.php]]''
 
 
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/tables/index.html]]''
 
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/tables/index.html]]''
 
* ''[[J3.2:Developing_a_MVC_Component/Using_the_database#admin/tables/helloworld.php|admin/tables/helloworld.php]]''
 
* ''[[J3.2:Developing_a_MVC_Component/Using_the_database#admin/tables/helloworld.php|admin/tables/helloworld.php]]''
  
Create a compressed file of this directory or directly download the [https://github.com/joomla/Joomla-3.2-Hello-World-Component/archive/step-7-basic-backend.zip archive] and install it using the extension manager of Joomla. You can add a menu item of this component using the menu manager in the backend.
+
Create a compressed file of this directory or directly download the [https://github.com/scionescire/Joomla-3.2-Hello-World-Component/archive/step-7-basic-backend.zip archive] and install it using the extension manager of Joomla. You can add a menu item of this component using the menu manager in the backend.
  
 
<span id="helloworld.xml">
 
<span id="helloworld.xml">
 
''helloworld.xml''
 
''helloworld.xml''
<source lang="xml">
+
<source lang="xml" highlight="13,56,63-64">
 
<?xml version="1.0" encoding="utf-8"?>
 
<?xml version="1.0" encoding="utf-8"?>
 
<extension type="component" version="3.2.0" method="upgrade">
 
<extension type="component" version="3.2.0" method="upgrade">
 +
 
<name>Hello World!</name>
 
<name>Hello World!</name>
 +
<!-- The following elements are optional and free of formatting constraints -->
 
<creationDate>January 2014</creationDate>
 
<creationDate>January 2014</creationDate>
 
<author>John Doe</author>
 
<author>John Doe</author>
Line 317: Line 319:
 
<copyright>Copyright Info</copyright>
 
<copyright>Copyright Info</copyright>
 
<license>License Info</license>
 
<license>License Info</license>
 +
<!--  The version string is recorded in the components table -->
 
<version>0.0.7</version>
 
<version>0.0.7</version>
 +
<!-- The description is optional and defaults to the name -->
 
<description>Description of the Hello World component ...</description>
 
<description>Description of the Hello World component ...</description>
  
Line 330: Line 334:
 
</sql>
 
</sql>
 
</uninstall>
 
</uninstall>
<update> <!-- Runs on update; New in 2.5 -->
+
<update> <!-- Runs on update; New since J2.5 -->
 
<schemas>
 
<schemas>
 
<schemapath type="mysql">sql/updates/mysql</schemapath>
 
<schemapath type="mysql">sql/updates/mysql</schemapath>
Line 336: Line 340:
 
</update>
 
</update>
  
 +
<!-- Site Main File Copy Section -->
 +
<!-- Note the folder attribute: This attribute describes the folder
 +
to copy FROM in the package to install therefore files copied
 +
in this section are copied from /site/ in the package -->
 
<files folder="site">
 
<files folder="site">
 
<filename>index.html</filename>
 
<filename>index.html</filename>
Line 345: Line 353:
  
 
<administration>
 
<administration>
<menu>Hello World!</menu>
+
<!-- Administration Menu Section -->
 +
<menu link='index.php?option=com_helloworld'>Hello World!</menu>
 +
<!-- Administration Main File Copy Section -->
 +
<!-- Note the folder attribute: This attribute describes the folder
 +
to copy FROM in the package to install therefore files copied
 +
in this section are copied from /admin/ in the package -->
 
<files folder="admin">
 
<files folder="admin">
 +
<!-- Admin Main File Copy Section -->
 
<filename>index.html</filename>
 
<filename>index.html</filename>
 
<filename>helloworld.php</filename>
 
<filename>helloworld.php</filename>
 
<filename>controller.php</filename>
 
<filename>controller.php</filename>
 +
<!-- SQL files section -->
 
<folder>sql</folder>
 
<folder>sql</folder>
 +
<!-- tables files section -->
 
<folder>tables</folder>
 
<folder>tables</folder>
 +
<!-- models files section -->
 
<folder>models</folder>
 
<folder>models</folder>
 
<!-- views files section -->
 
<!-- views files section -->
 
<folder>views</folder>
 
<folder>views</folder>
</files>
+
</files>
 
</administration>
 
</administration>
 +
 
</extension>
 
</extension>
 
</source>
 
</source>
Line 362: Line 380:
  
 
Now you can see in your component '''hello-world''' an array with two colums, two rows and checkboxes. You can click the checkboxes in order to select the different options you want.
 
Now you can see in your component '''hello-world''' an array with two colums, two rows and checkboxes. You can click the checkboxes in order to select the different options you want.
 +
 +
{{notice|Please create a pull request or issue at https://github.com/joomla/Joomla-3.2-Hello-World-Component for any code descprepancies or if editing any of the source code on this page.}}
  
 
{{:J3.2:Developing a MVC Component/Navigate
 
{{:J3.2:Developing a MVC Component/Navigate
Line 368: Line 388:
  
 
== Contributors ==
 
== Contributors ==
 +
*[[User:Gunjanpatel|Gunjan Patel]]
 
*[[User:cdemko|Christophe Demko]]
 
*[[User:cdemko|Christophe Demko]]
 
*[[User:oaksu|Ozgur Aksu]]
 
*[[User:oaksu|Ozgur Aksu]]
 
*[[User:presto|Preston Smith]]
 
*[[User:presto|Preston Smith]]
 +
*[[User:scionescire|Scionescire]]
  
 
[[Category:Joomla! 3.0]]
 
[[Category:Joomla! 3.0]]

Revision as of 16:22, 14 May 2015

Joomla! 
3.x
<translate> Tutorial</translate>
<translate> Developing an MVC Component</translate>

{{Chunk:Developing a Model-View-Controller (MVC) Component for Joomla!3.1 - Contents/<translate> en</translate>}} <translate> This is a multiple-article series of tutorials on how to develop a Model-View-Controller Component for Joomla! VersionJoomla 3.x.</translate>

<translate> Begin with the Introduction, and navigate the articles in this series by using the navigation button at the bottom or the box to the right (the Articles in This series).</translate>



This is a multiple-article series of tutorials on how to develop a Model-View-Contoller Component for Joomla! Version Joomla 3.10.

Introduction[edit]

This tutorial is part of the Developing a MVC Component for Joomla! 3.2 tutorial. You are encouraged to read the previous parts of the tutorial before reading this.

Basic backend[edit]

Designing the backend interface leads us to create at least a Model-View-Controller triptych. We have to modify the administrator entry point of our component, the admin/helloworld.php file

admin/helloworld.php

<?php
/**
 * @package     Joomla.Administrator
 * @subpackage  com_helloworld
 *
 * @copyright   Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// Get an instance of the controller prefixed by HelloWorld
$controller = JControllerLegacy::getInstance('HelloWorld');

// Perform the Request task
$input = JFactory::getApplication()->input;
$controller->execute($input->getCmd('task'));

// Redirect if set by the controller
$controller->redirect();

Create the general controller[edit]

The entry point now gets an instance of a HelloWorld prefixed controller. Let's create a basic controller for the administrator part:

admin/controller.php

<?php
/**
 * @package     Joomla.Administrator
 * @subpackage  com_helloworld
 *
 * @copyright   Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

/**
 * General Controller of HelloWorld component
 *
 * @package     Joomla.Administrator
 * @subpackage  com_helloworld
 * @since       0.0.7
 */
class HelloWorldController extends JControllerLegacy
{
	/**
	 * The default view for the display method.
	 *
	 * @var string
	 * @since 12.2
	 */
	protected $default_view = 'helloworlds';
}

This controller will display the 'HelloWorlds' view by default.

Create the view[edit]

With your favorite file manager and editor, create a file admin/views/helloworlds/view.html.php containing:

admin/views/helloworlds/view.html.php

<?php
/**
 * @package     Joomla.Administrator
 * @subpackage  com_helloworld
 *
 * @copyright   Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

/**
 * HelloWorlds View
 *
 * @since  0.0.1
 */
class HelloWorldViewHelloWorlds extends JViewLegacy
{
	/**
	 * Display the Hello World view
	 *
	 * @param   string  $tpl  The name of the template file to parse; automatically searches through the template paths.
	 *
	 * @return  void
	 */
	function display($tpl = null)
	{
		// Get data from the model
		$this->items		= $this->get('Items');
		$this->pagination	= $this->get('Pagination');

		// Check for errors.
		if (count($errors = $this->get('Errors')))
		{
			JError::raiseError(500, implode('<br />', $errors));

			return false;
		}

		// Display the template
		parent::display($tpl);
	}
}

In Joomla, views display data using layout. With your favorite file manager and editor, put a file admin/views/helloworlds/tmpl/default.php containing

admin/views/helloworlds/tmpl/default.php

<?php
/**
 * @package     Joomla.Administrator
 * @subpackage  com_helloworld
 *
 * @copyright   Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted Access');
?>
<form action="index.php?option=com_helloworld&view=helloworlds" method="post" id="adminForm" name="adminForm">
	<table class="table table-striped table-hover">
		<thead>
		<tr>
			<th width="1%"><?php echo JText::_('COM_HELLOWORLD_NUM'); ?></th>
			<th width="2%">
				<?php echo JHtml::_('grid.checkall'); ?>
			</th>
			<th width="90%">
				<?php echo JText::_('COM_HELLOWORLD_HELLOWORLDS_NAME') ;?>
			</th>
			<th width="5%">
				<?php echo JText::_('COM_HELLOWORLD_PUBLISHED'); ?>
			</th>
			<th width="2%">
				<?php echo JText::_('COM_HELLOWORLD_ID'); ?>
			</th>
		</tr>
		</thead>
		<tfoot>
			<tr>
				<td colspan="5">
					<?php echo $this->pagination->getListFooter(); ?>
				</td>
			</tr>
		</tfoot>
		<tbody>
			<?php if (!empty($this->items)) : ?>
				<?php foreach ($this->items as $i => $row) : ?>

					<tr>
						<td><?php echo $this->pagination->getRowOffset($i); ?></td>
						<td>
							<?php echo JHtml::_('grid.id', $i, $row->id); ?>
						</td>
						<td>
								<?php echo $row->greeting; ?>
						</td>
						<td align="center">
							<?php echo JHtml::_('jgrid.published', $row->published, $i, 'helloworlds.', true, 'cb'); ?>
						</td>
						<td align="center">
							<?php echo $row->id; ?>
						</td>
					</tr>
				<?php endforeach; ?>
			<?php endif; ?>
		</tbody>
	</table>
</form>

COM_HELLOWORLD_HELLOWORLDS_NAME, COM_HELLOWORLD_ID and the others are placeholders which will later be replaced with language-specific text. The JText::_ method translates a string into the current language.

checkAll is a javascript function defined in the Joomla core able to check all items.

JHtml::_ is a helper function able to display several HTML output. In this case, it will display a checkbox for the item.

JPagination is a Joomla class able to manage and display pagination object.

Create the model[edit]

The HelloWorlds view asks the model for data. In Joomla, there is a class able to manage a list of data: JModelList. Class JModelList and inherited classes need only one method:

  • getListQuery which constructs an SQL query

and two states:

  • list.start for determining the list offset
  • list.limit for determining the list length

The getItems and getPagination methods are defined in JModelList class. They don't need to be defined in the HelloWorldModelHelloWorlds class.

admin/models/helloworlds.php

<?php
/**
 * @package     Joomla.Administrator
 * @subpackage  com_helloworld
 *
 * @copyright   Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

/**
 * HelloWorldList Model
 *
 * @since  0.0.1
 */
class HelloWorldModelHelloWorlds extends JModelList
{
	/**
	 * Method to build an SQL query to load the list data.
	 *
	 * @return      string  An SQL query
	 */
	protected function getListQuery()
	{
		// Initialize variables.
		$db    = JFactory::getDbo();
		$query = $db->getQuery(true);

		// Create the base select statement.
		$query->select('*')
                ->from($db->quoteName('#__helloworld'));

		return $query;
	}
}

The _populateState method is, by default, automatically called when a state is read by the getState method.

Packaging the component[edit]

Content of your code directory

Create a compressed file of this directory or directly download the archive and install it using the extension manager of Joomla. You can add a menu item of this component using the menu manager in the backend.

helloworld.xml

<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="3.2.0" method="upgrade">

	<name>Hello World!</name>
	<!-- The following elements are optional and free of formatting constraints -->
	<creationDate>January 2014</creationDate>
	<author>John Doe</author>
	<authorEmail>john.doe@example.org</authorEmail>
	<authorUrl>http://www.example.org</authorUrl>
	<copyright>Copyright Info</copyright>
	<license>License Info</license>
	<!--  The version string is recorded in the components table -->
	<version>0.0.7</version>
	<!-- The description is optional and defaults to the name -->
	<description>Description of the Hello World component ...</description>

	<install> <!-- Runs on install -->
		<sql>
			<file driver="mysql" charset="utf8">sql/install.mysql.utf8.sql</file>
		</sql>
	</install>
	<uninstall> <!-- Runs on uninstall -->
		<sql>
			<file driver="mysql" charset="utf8">sql/uninstall.mysql.utf8.sql</file>
		</sql>
	</uninstall>
	<update> <!-- Runs on update; New since J2.5 -->
		<schemas>
			<schemapath type="mysql">sql/updates/mysql</schemapath>
		</schemas>
	</update>

	<!-- Site Main File Copy Section -->
	<!-- Note the folder attribute: This attribute describes the folder
		to copy FROM in the package to install therefore files copied
		in this section are copied from /site/ in the package -->
	<files folder="site">
		<filename>index.html</filename>
		<filename>helloworld.php</filename>
		<filename>controller.php</filename>
		<folder>views</folder>
		<folder>models</folder>
	</files>

	<administration>
		<!-- Administration Menu Section -->
		<menu link='index.php?option=com_helloworld'>Hello World!</menu>
		<!-- Administration Main File Copy Section -->
		<!-- Note the folder attribute: This attribute describes the folder
			to copy FROM in the package to install therefore files copied
			in this section are copied from /admin/ in the package -->
		<files folder="admin">
			<!-- Admin Main File Copy Section -->
			<filename>index.html</filename>
			<filename>helloworld.php</filename>
			<filename>controller.php</filename>
			<!-- SQL files section -->
			<folder>sql</folder>
			<!-- tables files section -->
			<folder>tables</folder>
			<!-- models files section -->
			<folder>models</folder>
			<!-- views files section -->
			<folder>views</folder>
		</files>
	</administration>

</extension>

Now you can see in your component hello-world an array with two colums, two rows and checkboxes. You can click the checkboxes in order to select the different options you want.

Info non-talk.png
General Information

Please create a pull request or issue at https://github.com/joomla/Joomla-3.2-Hello-World-Component for any code descprepancies or if editing any of the source code on this page.

J3.x:Developing a MVC Component/Navigate

Contributors[edit]