J3.x

Developing an MVC Component/Adding a model to the site part

From Joomla! Documentation

< J3.x:Developing an MVC Component
Revision as of 14:55, 18 September 2015 by Kevinkabatra (talk | contribs) (Finished Adding a Model to Hello World!, File Details, and Code Explanation sections)
Documentation all together tranparent small.png
Under Construction

This article or section is in the process of an expansion or major restructuring. You are welcome to assist in its construction by editing it as well. If this article or section has not been edited in several days, please remove this template.
This article was last edited by Kevinkabatra (talk| contribs) 8 years ago. (Purge)


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



Note[edit]

  • You can follow the steps below to add a view to the Hello World! component, or you can directly download the archive

Adding a Model to Hello World![edit]

In this article we will cover how to add a view to a basic Joomla! component. For this example we will be continuing our work on the Hello World! component.

There are several ways to update a Joomla! component. As with the last tutorial, we will focus option 2.

1 Manually add the files into <path_to_joomla>/
2 Update using the Joomla! Extension Manager and the original directory, non-compressed, used for the component install
3 Update using the Joomla! Extension Manager and an Update Server

To add a menu item you will need to navigate to com_helloworld, which is the original directory we made for our component. You must use the updated directory structure from the last tutorial. Using your preferred file manager, create or update the following files; as you create or update the files, add the source code for each file which is found in File Details.

1 Create: test.php <path_to_com_helloworld>/site/models/test.php
2 Create: index.html <path_to_com_helloworld>/site/models/index.html
3 Update: view.html.php <path_to_com_helloworld>/site/views/helloworld/view.html.php
4 Update: helloworld.xml <path_to_com_helloworld>/helloworld.xml

Updating the Hello World! Component[edit]

To update the Hello World! Component in the Joomla! website, please follow the same steps for the original installation.

File Details[edit]

site/models/test.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');

/**
 * HelloWorld Model
 *
 * @since  0.0.1
 */
class HelloWorldModelHelloWorld extends JModelItem
{
	/**
	 * @var string message
	 */
	protected $message;

	/**
	 * Get the message
         *
	 * @return  string  The message to be displayed to the user
	 */
	public function getMsg()
	{
		if (!isset($this->message))
		{
			$this->message = 'Hello World!';
		}

		return $this->message;
	}
}

site/views/helloworld/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');

/**
 * HTML View class for the HelloWorld Component
 *
 * @since  0.0.1
 */
class HelloWorldViewHelloWorld 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)
	{
		// Assign data to the view
		$this->msg = $this->get('Msg');

		// Check for errors.
		if (count($errors = $this->get('Errors')))
		{
			JLog::add(implode('<br />', $errors), JLog::WARNING, 'jerror');

			return false;
		}

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

site/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.4</version>
	<!-- The description is optional and defaults to the name -->
	<description>Description of the Hello World component ...</description>

	<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>
			<!-- SQL files section -->
			<folder>sql</folder>
		</files>
	</administration>

</extension>

Code Explanation[edit]

In case you were curious as to why this works the way it does.

test.php[edit]

class HelloWorldModelHelloWorld extends JModelItem
{

This class will be called by the class HelloWorldViewHelloWorld.

view.html.php[edit]

$this->msg = $this->get('Msg');

The HelloWorldViewHelloWorld class asks the model for data using the get method of the JViewLegacy.

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.

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]