J3.x

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

From Joomla! Documentation

< J3.x:Developing an MVC Component
m (high lights file name than beginner easily to understand)
(Marked this version for translation)
(10 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{:J3.1:Developing a MVC Component}}
+
<noinclude><languages /></noinclude>
 +
{{:J3.1:Developing an MVC Component/<translate><!--T:1-->
 +
en</translate>}}
 +
<translate>== Introduction == <!--T:2--></translate>
 +
<translate><!--T:3-->
 +
This tutorial is part of the [[S:MyLanguage/J3.2:Developing an MVC Component | Developing an MVC Component for Joomla! 3.2]] tutorial. You are encouraged to read the previous parts of the tutorial before reading this.</translate>
  
{{review}}
+
<translate>== Basic backend == <!--T:4--></translate>
 
+
<translate><!--T:5-->
This is a multiple-article series of tutorials on how to develop a Model-View-Contoller [[Component]] for Joomla! Version {{JVer|{{CurrentSTSVer|minor}}}}.
+
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 <tt>admin/helloworld.php</tt> file.</translate>
 
 
== Introduction ==
 
This tutorial is part of the [[J3.2:Developing a MVC Component | 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 ==
 
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
 
  
 
<span id="admin/helloworld.php">
 
<span id="admin/helloworld.php">
'''''admin/helloworld.php'''''
+
<tt>'''admin/helloworld.php'''</tt>
 
<source lang="php">
 
<source lang="php">
 
<?php
 
<?php
Line 30: Line 29:
  
 
// Perform the Request task
 
// Perform the Request task
$input = JFactory::getApplication()->input;
+
$controller->execute(JFactory::getApplication()->input->get('task'));
$controller->execute($input->getCmd('task'));
 
  
 
// Redirect if set by the controller
 
// Redirect if set by the controller
Line 39: Line 37:
 
</span>
 
</span>
  
== Create the general controller ==
+
<translate>== Create the general controller == <!--T:30-->
The entry point now gets an instance of a ''HelloWorld'' prefixed controller. Let's create a basic controller for the administrator part:
+
The entry point now gets an instance of a ''HelloWorld'' prefixed controller. Let's create a basic controller for the administrator part:</translate>
  
 
<span id="admin/controller.php">
 
<span id="admin/controller.php">
Line 78: Line 76:
 
</span>
 
</span>
  
This controller will display the 'HelloWorlds' view by default.
+
<translate><!--T:6-->
 +
This controller will display the 'HelloWorlds' view by default.</translate>
  
== Create the view ==
+
<translate>== Create the view == <!--T:7--></translate>
  
With your favorite file manager and editor, create a file ''admin/views/helloworlds/view.html.php'' containing:
+
<translate><!--T:8-->
 +
With your favorite file manager and editor, create a file <tt>admin/views/helloworlds/view.html.php</tt> containing:</translate>
  
 
<span id="admin/views/helloworlds/view.html.php">
 
<span id="admin/views/helloworlds/view.html.php">
''admin/views/helloworlds/view.html.php''
+
<tt>admin/views/helloworlds/view.html.php</tt>
 
<source lang="php">
 
<source lang="php">
 
<?php
 
<?php
Line 134: Line 134:
 
</span>
 
</span>
  
In Joomla, views display data using layout. With your favorite file manager and editor, put a file ''admin/views/helloworlds/tmpl/default.php'' containing
+
<translate><!--T:9-->
 +
In Joomla, views display data using layout. With your favorite file manager and editor, put a file <tt>admin/views/helloworlds/tmpl/default.php</tt> containing</translate>
  
 
<span id="admin/views/helloworlds/tmpl/default.php">
 
<span id="admin/views/helloworlds/tmpl/default.php">
''admin/views/helloworlds/tmpl/default.php''
+
<tt>admin/views/helloworlds/tmpl/default.php</tt>
 
<source lang="php">
 
<source lang="php">
 
<?php
 
<?php
Line 182: Line 183:
  
 
<tr>
 
<tr>
<td><?php echo $this->pagination->getRowOffset($i); ?></td>
+
<td>
 +
<?php echo $this->pagination->getRowOffset($i); ?>
 +
</td>
 
<td>
 
<td>
 
<?php echo JHtml::_('grid.id', $i, $row->id); ?>
 
<?php echo JHtml::_('grid.id', $i, $row->id); ?>
 
</td>
 
</td>
 
<td>
 
<td>
<?php echo $row->greeting; ?>
+
<?php echo $row->greeting; ?>
 
</td>
 
</td>
 
<td align="center">
 
<td align="center">
Line 205: Line 208:
 
</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.
+
<translate><!--T:10-->
 +
''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.</translate>
  
''checkAll'' is a javascript function defined in the Joomla core able to check all items.
+
<translate><!--T:11-->
 +
<tt>checkAll</tt> is a javascript function defined in the Joomla core able to check all items.</translate>
  
''JHtml::_'' is a helper function able to display several HTML output. In this case, it will display a checkbox for the item.
+
<translate><!--T:12-->
 +
<tt>JHtml::_</tt> is a helper function able to display several HTML output. In this case, it will display a checkbox for the item.</translate>
  
''JPagination'' is a Joomla class able to manage and display pagination object.
+
<translate><!--T:13-->
 +
<tt>JPagination</tt> is a Joomla class able to manage and display pagination object.</translate>
  
== Create the model ==
+
<translate>== Create the model == <!--T:14--></translate>
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:
+
<translate><!--T:15-->
* ''getListQuery'' which constructs an SQL query
+
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:</translate>
 +
<translate><!--T:16-->
 +
* ''getListQuery'' which constructs an SQL query</translate>
  
and two states:
+
<translate><!--T:17-->
* ''list.start'' for determining the list offset
+
and two states:</translate>
* ''list.limit'' for determining the list length
+
<translate><!--T:18-->
 +
* <tt>list.start</tt> for determining the list offset
 +
* <tt>list.limit</tt> for determining the list length</translate>
  
The ''getItems'' and ''getPagination'' methods are defined in JModelList class. They don't need to be defined in the ''HelloWorldModelHelloWorlds'' class.
+
<translate><!--T:19-->
 +
The <tt>getItems</tt> and <tt>getPagination</tt> methods are defined in JModelList class. They don't need to be defined in the <tt>HelloWorldModelHelloWorlds</tt> class.</translate>
  
 
<span id="admin/models/helloworlds.php">
 
<span id="admin/models/helloworlds.php">
'''''admin/models/helloworlds.php'''''
+
<tt>'''admin/models/helloworlds.php'''</tt>
 
<source lang="php">
 
<source lang="php">
 
<?php
 
<?php
Line 266: Line 278:
 
</span>
 
</span>
  
The ''_populateState'' method is, by default, automatically called when a state is read by the ''getState'' method.
+
<translate><!--T:20-->
 +
The <tt>_populateState</tt> method is, by default, automatically called when a state is read by the <tt>getState</tt> method.</translate>
 +
 
 +
<translate>== Packaging the component == <!--T:21--></translate>
  
== Packaging the component ==
+
<translate><!--T:22-->
 +
Content of your code directory</translate>
  
Content of your code directory
 
 
* ''[[#helloworld.xml|helloworld.xml]]''
 
* ''[[#helloworld.xml|helloworld.xml]]''
* ''[[J3.2:Developing_a_MVC_Component/Adding_a_view_to_the_site_part#site/helloworld.php|site/helloworld.php]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Adding_a_view_to_the_site_part#site/helloworld.php|site/helloworld.php]]''
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|site/index.html]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|site/index.html]]''
* ''[[J3.2:Developing_a_MVC_Component/Adding_a_view_to_the_site_part#site/controller.php|site/controller.php]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Adding_a_view_to_the_site_part#site/controller.php|site/controller.php]]''
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|site/views/index.html]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|site/views/index.html]]''
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|site/views/helloworld/index.html]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|site/views/helloworld/index.html]]''
* ''[[J3.2:Developing_a_MVC_Component/Adding_a_view_to_the_site_part#site/views/helloworld/view.html.php|site/views/helloworld/view.html.php]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Adding_a_view_to_the_site_part#site/views/helloworld/view.html.php|site/views/helloworld/view.html.php]]''
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|site/views/helloworld/tmpl/index.html]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|site/views/helloworld/tmpl/index.html]]''
* ''[[J3.2:Developing_a_MVC_Component/Using_the_database#site/views/helloworld/tmpl/default.xml|site/views/helloworld/tmpl/default.xml]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Using_the_database#site/views/helloworld/tmpl/default.xml|site/views/helloworld/tmpl/default.xml]]''
* ''[[J3.2:Developing_a_MVC_Component/Adding_a_view_to_the_site_part#site/views/helloworld/tmpl/default.php|site/views/helloworld/tmpl/default.php]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Adding_a_view_to_the_site_part#site/views/helloworld/tmpl/default.php|site/views/helloworld/tmpl/default.php]]''
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|site/models/index.html]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|site/models/index.html]]''
* ''[[J3.2:Developing_a_MVC_Component/Using_the_database#site/models/helloworld.php|site/models/helloworld.php]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Using_the_database#site/models/helloworld.php|site/models/helloworld.php]]''
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/index.html]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/index.html]]''
 
* ''[[#admin/helloworld.php|admin/helloworld.php]]''
 
* ''[[#admin/helloworld.php|admin/helloworld.php]]''
 
* ''[[#admin/controller.php|admin/controller.php]]''
 
* ''[[#admin/controller.php|admin/controller.php]]''
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/index.html]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/index.html]]''
* ''[[J3.2:Developing_a_MVC_Component/Using_the_database#admin/sql/install.mysql.utf8.sql|admin/sql/install.mysql.utf8.sql]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Using_the_database#admin/sql/install.mysql.utf8.sql|admin/sql/install.mysql.utf8.sql]]''
* ''[[J3.2:Developing_a_MVC_Component/Using_the_database#admin/sql/uninstall.mysql.utf8.sql|admin/sql/uninstall.mysql.utf8.sql]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Using_the_database#admin/sql/uninstall.mysql.utf8.sql|admin/sql/uninstall.mysql.utf8.sql]]''
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/updates/index.html]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/updates/index.html]]''
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/updates/mysql/index.html]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/updates/mysql/index.html]]''
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#admin/sql/updates/mysql/0.0.1.sql|admin/sql/updates/mysql/0.0.1.sql]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#admin/sql/updates/mysql/0.0.1.sql|admin/sql/updates/mysql/0.0.1.sql]]''
* ''[[J3.2:Developing_a_MVC_Component/Using_the_database#admin/sql/install.mysql.utf8.sql|admin/sql/updates/mysql/0.0.6.sql]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Using_the_database#admin/sql/install.mysql.utf8.sql|admin/sql/updates/mysql/0.0.6.sql]]''
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/models/index.html]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/models/index.html]]''
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/models/fields/index.html]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/models/fields/index.html]]''
* ''[[J3.2:Developing_a_MVC_Component/Using_the_database#admin/models/fields/helloworld.php|admin/models/fields/helloworld.php]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Using_the_database#admin/models/fields/helloworld.php|admin/models/fields/helloworld.php]]''
 
* ''[[#admin/models/helloworlds.php|admin/models/helloworlds.php]]''
 
* ''[[#admin/models/helloworlds.php|admin/models/helloworlds.php]]''
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/views/index.html]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/views/index.html]]''
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/views/helloworlds/index.html]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/views/helloworlds/index.html]]''
 
* ''[[#admin/views/helloworlds/view.html.php|admin/views/helloworlds/view.html.php]]''
 
* ''[[#admin/views/helloworlds/view.html.php|admin/views/helloworlds/view.html.php]]''
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/views/helloworlds/tmpl/index.html]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_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]]''
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/tables/index.html]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_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]]''
+
* ''[[S:MyLanguage/J3.2:Developing_an_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/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.
+
<translate><!--T:23-->
 +
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.</translate>
  
 
<span id="helloworld.xml">
 
<span id="helloworld.xml">
''helloworld.xml''
+
<tt>helloworld.xml</tt>
 
<source lang="xml" highlight="13,56,63-64">
 
<source lang="xml" highlight="13,56,63-64">
 
<?xml version="1.0" encoding="utf-8"?>
 
<?xml version="1.0" encoding="utf-8"?>
Line 381: Line 397:
 
</span>
 
</span>
  
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.
+
<translate><!--T:24-->
 
+
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.</translate>
{{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
+
{{notice|<translate><!--T:25-->
|prev=Using the database <!-- previous article subpage name -->
+
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.</translate>}}
|next=Adding language management <!-- next article subpage name -->}}
 
  
 
== Contributors ==
 
== Contributors ==
Line 396: Line 410:
 
*[[User:scionescire|Scionescire]]
 
*[[User:scionescire|Scionescire]]
  
 +
<div class="row">
 +
<div class="large-6 columns">{{Basic button|<translate><!--T:26-->
 +
S:MyLanguage/J3.x:Developing_an_MVC_Component/Using the database|Prev: Using the database</translate>|class=expand success}}</div>
 +
<div class="large-6 columns">{{Basic button|<translate><!--T:27-->
 +
S:MyLanguage/J3.x:Developing_an_MVC_Component/Adding language management|Next: Adding language management</translate>|class=expand}}</div>
 +
</div>
 +
 +
<noinclude>
 +
<translate>
 +
<!--T:29-->
 +
[[Category:Joomla! 3.x]]
 
[[Category:Joomla! 3.0]]
 
[[Category:Joomla! 3.0]]
 
[[Category:Joomla! 3.1]]
 
[[Category:Joomla! 3.1]]
 
[[Category:Joomla! 3.2]]
 
[[Category:Joomla! 3.2]]
 +
[[Category:Joomla! 3.3]]
 +
[[Category:Joomla! 3.4]]
 
[[Category:Beginner Development]]
 
[[Category:Beginner Development]]
 
[[Category:Component Development]]
 
[[Category:Component Development]]
 +
[[Category:Tutorials]]
 +
[[Category:Tutorials in a Series]]
 +
</translate>
 +
</noinclude>

Revision as of 14:03, 22 July 2015

Other languages:
English • ‎Nederlands • ‎español • ‎français • ‎العربية • ‎中文(台灣)‎
Joomla! 
3.x
Tutorial
Developing an MVC Component



This is a multiple-article series of tutorials on how to develop a Model-View-Controller Component for Joomla! VersionJoomla 3.x.

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).



Introduction[edit]

This tutorial is part of the Developing an 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
$controller->execute(JFactory::getApplication()->input->get('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.

Contributors[edit]