J3.x

Difference between revisions of "Developing an MVC Component/Adding a model to the site part"

From Joomla! Documentation

< J3.x:Developing an MVC Component
(favourite instead of favorite)
m (Removed {{underconstruction}} tag)
(6 intermediate revisions by the same user not shown)
Line 2: Line 2:
 
{{:J3.1:Developing an MVC Component/<translate><!--T:23-->
 
{{:J3.1:Developing an MVC Component/<translate><!--T:23-->
 
en</translate>}}
 
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 a MVC Component for Joomla! 3.2]] tutorial. You are encouraged to read the previous parts of the tutorial before reading this.</translate>
 
<translate>== Adding a model == <!--T:4--></translate>
 
<translate><!--T:5-->
 
In the Joomla framework, models are responsible for managing the data. The first function that has to be written for a model is a ''get'' function. It returns data to the caller. In our case, the caller will be the ''HelloWorldViewHelloWorld'' view. By default, the model named ''HelloWorldModelHelloWorld'' residing in ''site/models/helloworld.php'' is the main model associated to this view.</translate>
 
  
<translate><!--T:6-->
+
== Note ==
So let's have a quick look at the naming conventions with an example, since the naming convention are the actual magic, that make everything work:</translate>
+
* This tutorial is part of the [[S:MyLanguage/J3.x:Developing_an_MVC_Component/Introduction|Developing a MVC Component for Joomla! 3.x]] tutorial. You are encouraged to read the previous parts of the tutorial before reading this.
  
<translate><!--T:7-->
+
* You can follow the steps below to add a view to the Hello World! component, or you can directly download the [https://github.com/scionescire/Joomla-3.2-Hello-World-Component/archive/step-4-adding-a-site-model.zip archive]
The class ''HelloWorldView'''HelloWorld''''' resides in ''site/views/'''helloworld'''/view.html.php'' and will make use of the class ''HelloWorldModel'''HelloWorld''''' in the file ''site/models/'''helloworld'''.php''</translate>
 
  
<translate><!--T:8-->
+
== Adding a Model to Hello World! ==
So let's just assume we want to use an imaginary view ''fluffy'', you would have to have:</translate>
+
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 [[J3.x:Developing_an_MVC_Component/Developing_a_Basic_Component| Hello World!]] component.
  
<translate><!--T:9-->
+
There are several ways to update a Joomla! component. As with the last tutorial, we will focus option 2.
The class ''HelloWorldView'''Fluffy''''' which resides in ''site/views/'''fluffy'''/view.html.php''. The view will make use of ''HelloWorldModel'''Fluffy''''' in the file ''site/models/'''fluffy'''.php''. Note: the actual screen of the view: ''site/views/'''fluffy'''/tmpl/default.php'' is required as well to make this example work.</translate>
+
{| border=1
 +
| 1
 +
| Manually add the files into <tt><path_to_joomla>/</tt>
 +
|-
 +
| 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 [[Deploying_an_Update_Server| Update Server]]
 +
|}
  
<translate><!--T:10-->
+
To add a menu item you will need to navigate to <tt>com_helloworld</tt>, which is the original directory we made for our component. You must use the updated directory structure from the last [[J3.x:Developing_an_MVC_Component/Adding_a_menu_type_to_the_site_part | 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|File Details]].
Breaking any of these bold conventions will lead to errors or a blank page.</translate>
 
  
<translate><!--T:11-->
+
{| border=1
So back to the actual implementation of the single classes:</translate>
+
| 1
 +
| Create: [[#site/models/test.php| test.php]]
 +
| <tt><path_to_com_helloworld>/site/models/test.php<tt>
 +
|-
 +
| 2
 +
| Create: [[#site/models/index.html| index.html]]
 +
| <tt><path_to_com_helloworld>/site/models/index.html</tt>
 +
|-
 +
| 3
 +
| Update: [[#site/views/helloworld/view.html.php| view.html.php]]
 +
| <tt><path_to_com_helloworld>/site/views/helloworld/view.html.php</tt>
 +
|-
 +
| 4
 +
| Update: [[#helloworld.xml| helloworld.xml]]
 +
| <tt><path_to_com_helloworld>/helloworld.xml</tt>
 +
|}
  
<translate><!--T:12-->
+
=== Updating the Hello World! Component ===
With your favourite file manager and editor put a ''site/models/helloworld.php'' file containing:</translate>
+
To update the Hello World! Component in the Joomla! website, please follow the same steps for the [[J3.x:Developing_an_MVC_Component/Developing_a_Basic_Component#Installing the Hello World! Component |original installation]].
 
 
<span id="site/models/helloworld.php">
 
<tt>'''''site/models/helloworld.php'''''</tt>
 
  
 +
== File Details ==
 +
{{vanchor|site/models/test.php}}
 
<source lang="php">
 
<source lang="php">
 
<?php
 
<?php
Line 74: Line 89:
 
}
 
}
 
</source>
 
</source>
</span>
 
 
<translate><!--T:13-->
 
The ''HelloWorldViewHelloWorld'' class asks the model for data using the ''get'' method of the ''JViewLegacy'' class:</translate>
 
 
<span id="site/views/helloworld/view.html.php">
 
<tt>'''''site/views/helloworld/view.html.php'''''</tt>
 
  
 +
{{vanchor|site/views/helloworld/view.html.php}}
 
<source lang="php" highlight="30">
 
<source lang="php" highlight="30">
 
<?php
 
<?php
Line 126: Line 135:
 
}
 
}
 
}
 
}
 
 
</source>
 
</source>
</span>
 
 
<translate><!--T:14-->
 
Note: $this->get() is a member of JViewLegacy::get which is a proxy to get* methods of the default model where * is populated with the value of the first parameter passed to get()</translate>
 
 
<translate><!--T:15-->
 
Also modify your ''helloworld.xml'' file to indicate use of models and the new version:</translate>
 
 
<span id="helloworld.xml">
 
<tt>'''''helloworld.xml'''''</tt>
 
  
 +
{{vanchor|site/helloworld.xml}}
 
<source lang="xml" highlight="13,32">
 
<source lang="xml" highlight="13,32">
 
<?xml version="1.0" encoding="utf-8"?>
 
<?xml version="1.0" encoding="utf-8"?>
Line 192: Line 191:
 
</extension>
 
</extension>
 
</source>
 
</source>
</span>
 
  
<translate>== Packaging the component == <!--T:27-->  
+
== Code Explanation ==
 +
In case you were curious as to why this works the way it does.
 +
 
 +
=== test.php ===
 +
 
 +
<source lang="php">
 +
class HelloWorldModelHelloWorld extends JModelItem
 +
{
 +
</source>
 +
 
 +
This class will be called by the class HelloWorldViewHelloWorld.
 +
 
 +
=== view.html.php ===
 +
 
 +
<source lang="php">
 +
$this->msg = $this->get('Msg');
 +
</source>
 +
 
 +
The ''HelloWorldViewHelloWorld'' class asks the model for data using the ''get'' method of the ''JViewLegacy''.
  
<!--T:16-->
+
== Component Contents ==
Content of your code directory</translate>
+
At this point in the tutorial, your component should contain the following files:
* ''[[#helloworld.xml|helloworld.xml]]''
+
{| border=1
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Adding_a_view_to_the_site_part#site/helloworld.php|site/helloworld.php]]''
+
| 1
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|site/index.html]]''
+
| ''[[#helloworld.xml|helloworld.xml]]''
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Adding_a_view_to_the_site_part#site/controller.php|site/controller.php]]''
+
| this is an XML (manifest) file that tells Joomla! how to install our component.
* ''[[S:MyLanguage/J3.2:Developing_an_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/helloworld/index.html]]''
+
| 2
* ''[[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]]''
+
| ''[[#site/helloworld.php|site/helloworld.php]]''
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|site/views/helloworld/tmpl/index.html]]''
+
| this is the site entry point to the Hello World! component
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Adding_a_menu_type_to_the_site_part#site/views/helloworld/tmpl/default.xml|site/views/helloworld/tmpl/default.xml]]''
+
|-
* ''[[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]]''
+
| 3
* ''[[S:MyLanguage/J3.2:Developing_an_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/index.html]]''
* ''[[#site/models/helloworld.php|site/models/helloworld.php]]''
+
| prevents web server from listing directory content
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/index.html]]''
+
|-
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#admin/helloworld.php|admin/helloworld.php]]''
+
| 4
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/index.html]]''
+
| ''[[#site/controller.php|site/controller.php]]''
* ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/updates/index.html]]''
+
| file representing the controller
* ''[[S:MyLanguage/J3.2:Developing_an_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#admin/sql/updates/mysql/0.0.1.sql|admin/sql/updates/mysql/0.0.1.sql]]''
+
| 5
<translate><!--T:17-->
+
| ''[[#site/models/test.php|site/models/test.php]]''
Create a compressed file of this directory or directly download the [https://github.com/scionescire/Joomla-3.2-Hello-World-Component/archive/step-4-adding-a-site-model.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>
+
| file representing the model
 +
|-
 +
|6
 +
| ''[[#site/models/index.html|site/models/index.html]]''
 +
| prevents web server from listing directory content
 +
|-
 +
| 7
 +
| ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|site/views/index.html]]''
 +
| prevents web server from listing directory content
 +
|-
 +
| 8
 +
| ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|site/views/helloworld/index.html]]''
 +
| prevents web server from listing directory content
 +
|-
 +
| 9
 +
| ''[[#site/views/helloworld/view.html.php|site/views/helloworld/view.html.php]]''
 +
| file representing the view
 +
|-
 +
| 10
 +
| ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|site/views/helloworld/tmpl/index.html]]''
 +
| prevents web server from listing directory content
 +
|-
 +
| 11
 +
| ''[[#site/views/helloworld/tmpl/default.php|site/views/helloworld/tmpl/default.php]]''  
 +
| the default view
 +
|-
 +
| 12
 +
| ''[[#site/views/helloworld/tmpl/default.xml|site/views/helloworld/tmpl/default.xml]]''
 +
| file that adds menu item
 +
|-
 +
| 13
 +
| ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/index.html]]''
 +
| prevents web server from listing directory content
 +
|-
 +
| 14
 +
| ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#admin/helloworld.php|admin/helloworld.php]]''
 +
| this is the administrator entry point to the Hello World! component
 +
|-
 +
| 15
 +
| ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/index.html]]''
 +
| prevents web server from listing directory content
 +
|-
 +
| 16
 +
| ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/updates/index.html]]''
 +
| prevents web server from listing directory content
 +
|-
 +
| 17
 +
| ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/updates/mysql/index.html]]''
 +
| prevents web server from listing directory content
 +
|-
 +
| 18
 +
| ''[[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]]''
 +
| file allowing to initialise schema version of the com_helloworld component.
 +
|}
  
 
{{notice|<translate><!--T:18-->
 
{{notice|<translate><!--T:18-->
Line 228: Line 297:
 
*[[User:betweenbrain|Matt Thomas]]
 
*[[User:betweenbrain|Matt Thomas]]
 
*[[User:Scionescire|Scionescire]]
 
*[[User:Scionescire|Scionescire]]
 +
*[[User:Kevinkabatra|Kevin Kabatra]]
  
 
<div class="row">  
 
<div class="row">  

Revision as of 15:04, 18 September 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).



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.

Component Contents[edit]

At this point in the tutorial, your component should contain the following files:

1 helloworld.xml this is an XML (manifest) file that tells Joomla! how to install our component.
2 site/helloworld.php this is the site entry point to the Hello World! component
3 site/index.html prevents web server from listing directory content
4 site/controller.php file representing the controller
5 site/models/test.php file representing the model
6 site/models/index.html prevents web server from listing directory content
7 site/views/index.html prevents web server from listing directory content
8 site/views/helloworld/index.html prevents web server from listing directory content
9 site/views/helloworld/view.html.php file representing the view
10 site/views/helloworld/tmpl/index.html prevents web server from listing directory content
11 site/views/helloworld/tmpl/default.php the default view
12 site/views/helloworld/tmpl/default.xml file that adds menu item
13 admin/index.html prevents web server from listing directory content
14 admin/helloworld.php this is the administrator entry point to the Hello World! component
15 admin/sql/index.html prevents web server from listing directory content
16 admin/sql/updates/index.html prevents web server from listing directory content
17 admin/sql/updates/mysql/index.html prevents web server from listing directory content
18 admin/sql/updates/mysql/0.0.1.sql file allowing to initialise schema version of the com_helloworld component.
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]